Android:纵向模式下的相机预览方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10660598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Camera preview orientation in portrait mode
提问by DominicM
I'm using the camera to show preview only (not to take pictures or record videos).
我使用相机仅显示预览(不拍照或录制视频)。
The app is always in portrait mode (landscape mode is disabled).
The camera preview is always rotated 90 degrees ccw and I can't change it
(neither with setDisplayOrientation
nor with p.set("orientation", "portrait" )
and p.set("rotation", 90) )
.
该应用程序始终处于纵向模式(禁用横向模式)。相机预览总是旋转90度逆时针,我不能既不改变它(setDisplayOrientation
也有p.set("orientation", "portrait" )
和p.set("rotation", 90) )
。
Is the preview ALWAYS rotated like this or is it device dependent? If it was always like this in portrait mode, I could just rotate the image afterwards.
预览总是像这样旋转还是依赖于设备?如果在纵向模式下总是这样,我可以在之后旋转图像。
Or is there a way to set up the camera correctly? I've read a lot of threads about this but no answer worked for me (Galaxy s2, Android v2.3)
或者有没有办法正确设置相机?我已经阅读了很多关于此的主题,但没有答案对我有用(Galaxy s2,Android v2.3)
回答by smith324
To force portrait orientation:
强制纵向:
set android:screenOrientation="portrait"
in your AndroidManifest.xml
and call camera.setDisplayOrientation(90);
before calling camera.startPreview();
android:screenOrientation="portrait"
在AndroidManifest.xml
呼叫camera.setDisplayOrientation(90);
之前设置您的和呼叫camera.startPreview();
I have not tested on the GS2 but it works on every phone I have tested on.
我没有在 GS2 上测试过,但它适用于我测试过的每部手机。
Note: setDisplayOrientation was added in API Level 8
注意:setDisplayOrientation 是在API Level 8 中添加的
回答by vignesh waran
I have coded the app for only Portrait Mode.
我只为纵向模式编写了应用程序。
camera.setDisplayOrientation(90);
Will make the Camera to rotate to 90 degree and This may result in not suitable Orientation for some devices in android In order to get the Correct Preview for all android devices use the following code which is refereed in developers site.
会使相机旋转到 90 度,这可能会导致 android 中某些设备的方向不合适为了获得所有 android 设备的正确预览,请使用以下代码,该代码在开发人员站点中被引用。
Below you have to send your activity, cameraId = back is 0 and for Front camera is 1
下面您必须发送您的活动,cameraId = back 为 0,前置摄像头为 1
public static 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;
//int currentapiVersion = android.os.Build.VERSION.SDK_INT;
// do something for phones running an SDK before lollipop
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);
}
This is how to set the setDisplayOrientation for camera which will perfectly for all android devices.
这是为相机设置 setDisplayOrientation 的方法,这将完美适用于所有 android 设备。