1. Camera旋转问题。
了解这个问题需要区分2个旋转( 屏幕旋转,和预览图片的旋转)
先上图:
箭头方向代表正方向。
当屏幕左移90,那么预览的图片就要右移270。
这就是为什么
private int getPreviewDegree(Activity activity, CameraInfo info) {
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
mDegree = 0;
switch (rotation) {
case Surface.ROTATION_0:
mDegree = 0;
break;
case Surface.ROTATION_90:
mDegree = 90;
break;
case Surface.ROTATION_180:
mDegree = 180;
break;
case Surface.ROTATION_270:
mDegree = 270;
break;
}
int result = 0;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + mDegree) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - mDegree + 360) % 360;
}
return result;
}
的下面要加一个修正了。
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 2014/9/4 更新1
int orientation = mCameraInfo.orientation;
int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
Size size0 = parameters.getPictureSize();
if( rotation == 0 || rotation == 2 ) {
if( orientation == 90 ) {
mPreviewFL.setAspectRatio((double) size0.height / size0.width);
} else {
mPreviewFL.setAspectRatio((double) size0.width / size0.height);
}
} else if( rotation == 1 || rotation == 3) {
if( orientation == 90 ) {
mPreviewFL.setAspectRatio((double) size0.width / size0.height);
} else {
mPreviewFL.setAspectRatio((double) size0.height / size0.width);
}
}
这部分代码非常重要,他考虑了很多因素。
1. 摄像头默认角度,即getRotation里所说的"natural"的角度,结合下图,相信大家和我会更加明白这点。
在下图中,我们用int orientation = mCameraInfo.orientation;代表当前摄像头角度,左边pad的角度为0,右边pad的角度为90。
2. int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
这里返回的rotation: 1 -- 90度; 2 -- 180度; 3 -- 270度;它是根据pad物理旋转顺时针方向来计算的。
若顺时针旋转90度,那么rotation就是270度,即rotation为3;其他以此类推。
这是android考虑到要弥补绘制的内容和拍摄内容的偏差,而做了一个计算:(360 - 顺时针旋转角度) % 360。
3. getPictureSize返回的是拍摄的图片尺寸,如果你从来未设置过这个值,那么getPictureSize默认返回的是最大尺寸,无论屏幕方向如何,该尺寸总为‘ 宽 > 高'
在我的pad上,测试的结果为1600 : 1200
我会根据这个结果以及当前屏幕的方向,来计算父窗口的宽高比。