private void updateCameraParameters() {
if
(mCamera !=
null
) {
Camera.Parameters p = mCamera.getParameters();
long time =
new
Date().getTime();
p.setGpsTimestamp(time);
Size pictureSize = findBestPictureSize(p);
p.setPictureSize(pictureSize.width, pictureSize.height);
// Set the preview frame aspect ratio according to the picture size.
Size size = p.getPictureSize();
PreviewFrameLayout frameLayout = (PreviewFrameLayout) findViewById(R.id.frame_layout);
frameLayout.setAspectRatio((double) size.width / size.height);
Size previewSize = findBestPreviewSize(p);
p.setPreviewSize(previewSize.width,previewSize.height);
mCamera.setParameters(p);
int supportPreviewWidth = previewSize.width;
int supportPreviewHeight = previewSize.height;
int srcWidth = getScreenWH().widthPixels;
int srcHeight = getScreenWH().heightPixels;
int width = Math.min(srcWidth, srcHeight);
int height = width * supportPreviewWidth / supportPreviewHeight ;
mSur.setLayoutParams(
new
FrameLayout.LayoutParams(height, width));
//
}
}
private Size findBestPictureSize(Camera.Parameters parameters) {
int diff = Integer.MIN_VALUE;
String pictureSizeValueString = parameters.get(
"picture-size-values"
);
// saw this on Xperia
if
(pictureSizeValueString ==
null
) {
pictureSizeValueString = parameters.get(
"picture-size-value"
);
}
if
(pictureSizeValueString ==
null
) {
return
mCamera.
new
Size(getScreenWH().widthPixels,getScreenWH().heightPixels);
}
Log.d(
"tag"
,
"pictureSizeValueString : "
+ pictureSizeValueString);
int bestX = 0;
int bestY = 0;
for
(String pictureSizeString : COMMA_PATTERN.split(pictureSizeValueString))
{
pictureSizeString = pictureSizeString.trim();
int dimPosition = pictureSizeString.indexOf(
'x'
);
if
(dimPosition == -1){
Log.e(TAG,
"Bad pictureSizeString:"
+pictureSizeString);
continue
;
}
int newX = 0;
int newY = 0;
try
{
newX = Integer.parseInt(pictureSizeString.substring(0, dimPosition));
newY = Integer.parseInt(pictureSizeString.substring(dimPosition+1));
}
catch
(NumberFormatException e){
Log.e(TAG,
"Bad pictureSizeString:"
+pictureSizeString);
continue
;
}
Point screenResolution =
new
Point (getScreenWH().widthPixels,getScreenWH().heightPixels);
int newDiff = Math.abs(newX - screenResolution.x)+Math.abs(newY- screenResolution.y);
if
(newDiff == diff)
{
bestX = newX;
bestY = newY;
break
;
}
else
if
(newDiff > diff){
if
((3 * newX) == (4 * newY)) {
bestX = newX;
bestY = newY;
diff = newDiff;
}
}
}
if
(bestX > 0 && bestY > 0) {
return
mCamera.
new
Size(bestX, bestY);
}
return
null
;
}
private Size findBestPreviewSize(Camera.Parameters parameters) {
String previewSizeValueString =
null
;
int diff = Integer.MAX_VALUE;
previewSizeValueString = parameters.get(
"preview-size-values"
);
if
(previewSizeValueString ==
null
) {
previewSizeValueString = parameters.get(
"preview-size-value"
);
}
if
(previewSizeValueString ==
null
) {
// 有些手機(jī)例如m9獲取不到支持的預(yù)覽大小 就直接返回屏幕大小
return
mCamera.
new
Size(getScreenWH().widthPixels,getScreenWH().heightPixels);
}
Log.d(
"tag"
,
"previewSizeValueString : "
+ previewSizeValueString);
int bestX = 0;
int bestY = 0;
for
(String prewsizeString : COMMA_PATTERN.split(previewSizeValueString))
{
prewsizeString = prewsizeString.trim();
int dimPosition = prewsizeString.indexOf(
'x'
);
if
(dimPosition == -1){
Log.e(TAG,
"Bad prewsizeString:"
+prewsizeString);
continue
;
}
int newX = 0;
int newY = 0;
try
{
newX = Integer.parseInt(prewsizeString.substring(0, dimPosition));
newY = Integer.parseInt(prewsizeString.substring(dimPosition+1));
}
catch
(NumberFormatException e){
Log.e(TAG,
"Bad prewsizeString:"
+prewsizeString);
continue
;
}
Point screenResolution =
new
Point (getScreenWH().widthPixels,getScreenWH().heightPixels);
int newDiff = Math.abs(newX - screenResolution.x)+Math.abs(newY- screenResolution.y);
if
(newDiff == diff)
{
bestX = newX;
bestY = newY;
break
;
}
else
if
(newDiff < diff){
if
((3 * newX) == (4 * newY)) {
bestX = newX;
bestY = newY;
diff = newDiff;
}
}
}
if
(bestX > 0 && bestY > 0) {
return
mCamera.
new
Size(bestX, bestY);
}
return
null
;
}
protected DisplayMetrics getScreenWH() {
DisplayMetrics dMetrics =
new
DisplayMetrics();
dMetrics =
this
.getResources().getDisplayMetrics();
return
dMetrics;
}