在调试一款摄像头的过程中,最初的预览图像与实际角度相差90度,需要对预览角度进行修正,代码修改如下:
在PhotoModule.java中修改setDisplayOrientation()函数,将 mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation);改为mCameraDevice.setDisplayOrientation(mCameraDisplayOrientation+90);
这样修改之后,预览图像旋转了90度,但预览画面被拉伸,这是由于预览窗口的长宽比和预览图像的长宽比不匹配导致,所以接着修改。
在PhotoUI.java中修改如下:
int orientation = mActivity.getResources().getConfiguration().orientation;
if (((rotation == 0 || rotation == 180) && scaledTextureWidth > scaledTextureHeight)
|| ((rotation == 90 || rotation == 270)
&& scaledTextureWidth < scaledTextureHeight)) {
//lp = new FrameLayout.LayoutParams((int) scaledTextureHeight, (int) scaledTextureWidth, Gravity.CENTER);//原来的代码
lp = new FrameLayout.LayoutParams((int) scaledTextureWidth, (int) scaledTextureHeight, Gravity.CENTER);
} else {
lp = new FrameLayout.LayoutParams((int) scaledTextureHeight, (int) scaledTextureWidth, Gravity.CENTER);
// lp = new FrameLayout.LayoutParams((int) scaledTextureWidth, (int) scaledTextureHeight, Gravity.CENTER);//原来的代码
}
这样,预览图像正常了,有些是可以直接交换摄像头参数中的长宽比,但一些驱动不支持。
存储图像之后又发现存储下来的图像还是旋转了90度。继续修改。
又在PhotoModule.java中的capture()中修改存储方向:
//int orientation = mOrientation;
int orientation;
if(mActivity.isAutoRotateScreen()) {
orientation = (360 - mDisplayRotation + 90) % 360;
} else {
orientation = (mOrientation + 90) % 360;
}
这下全部OK,视频的角度旋转的处理方式一样。
在实际处理过程中要注意屏与摄像头的匹配,你不能竖屏配一下横屏显示,横屏配一个竖屏显示,如果这种情况可能就还需要模组厂重新打样调整sensor方向了