我的整个android相机应用程序都处于纵向模式,因此当我打开相机时,我需要它自动以纵向模式打开。当我现在打开相机时,预览是横向的。如何设置相机预览以纵向模式打开,以使预览看起来正确?
您可以使用Android Developer文档中的此方法来旋转相机预览。
公共最终空白setDisplayOrientation(以度为单位)
以度为单位设置预览显示的顺时针旋转。这会影响预览帧和快照后显示的图片。此方法对于人像模式应用程序很有用。请注意,前置摄像头的预览显示在旋转之前会水平翻转,也就是说,图像会沿摄像头传感器的中心垂直轴反射。因此,用户可以将自己看作是照镜子。
private void setCameraDisplayOrientation(Activity activity, int cameraId,
android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result = 0;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
我希望这有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句