Android:纵向模式下的相机预览方向

Dom*_*icM 16 camera android portrait rotation orientation

我正在使用相机仅显示预览(不拍照或录制视频).

应用程序始终处于纵向模式(禁用横向模式).相机预览总是旋转90度ccw我无法改变它(无论是setDisplayOrientation也不是p.set("orientation", "portrait" )p.set("rotation", 90) ).

预览总是像这样旋转还是依赖于设备?如果在纵向模式下总是如此,我可以随后旋转图像.

或者有没有办法正确设置相机?我已经阅读了很多有关此问题的帖子,但没有回答对我有用(Galaxy s2,Android v2.3)

smi*_*324 36

要强制纵向方向:

android:screenOrientation="portrait"在你AndroidManifest.xml打电话camera.setDisplayOrientation(90);之前设置并打电话camera.startPreview();

我没有在GS2上测试,但它适用于我测试的每部手机.

注意:在API级别8中添加了setDisplayOrientation

  • **这不适用于三星Ace**,这里我发布了问题http://stackoverflow.com/questions/19176038/camera-setdisplayorientation-function-is-not-working-for-samsung-galaxy-ace-wi (2认同)

小智 7

我只为肖像模式编写了应用程序.

camera.setDisplayOrientation(90);
Run Code Online (Sandbox Code Playgroud)

将使相机旋转到90度这可能导致Android中某些设备的方向不合适为了获得所有Android设备的正确预览,请使用以下代码,这些代码在开发人员站点中被引用.

下面你必须发送你的活动,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);
} 
Run Code Online (Sandbox Code Playgroud)

这是如何为相机设置setDisplayOrientation,它将完美适用于所有Android设备.