【问题标题】:Android: Camera preview orientation in portrait modeAndroid:纵向模式下的相机预览方向
【发布时间】:2012-05-18 22:41:14
【问题描述】:

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

应用始终处于纵向模式(横向模式已禁用)。 相机预览总是逆时针旋转 90 度,我无法更改它 (setDisplayOrientationp.set("orientation", "portrait" )p.set("rotation", 90) ) 都没有。

预览总是像这样旋转还是取决于设备?如果在纵向模式下总是这样,我可以在之后旋转图像。

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

【问题讨论】:

标签: android camera rotation orientation portrait


【解决方案1】:

强制纵向:

在您的AndroidManifest.xml 中设置android:screenOrientation="portrait" 并在调用camera.startPreview(); 之前调用camera.setDisplayOrientation(90);

我没有在 GS2 上进行过测试,但它适用于我测试过的每部手机。

注意:API Level 8 中添加了 setDisplayOrientation

【讨论】:

  • 这适用于模拟器,但不适用于我的 GS2。但如果我理解正确,相机总是必须在纵向模式下顺时针旋转 90°,对吗?因为那样我会自己旋转它。无论如何,这将是一种更好的方法,因为它可以在低于 8 的 API 级别上工作。
  • 这在 Samsung Ace 上不起作用,我在这里发布了问题 stackoverflow.com/questions/19176038/…
  • 它不必总是旋转 90 度。在这里查看答案:stackoverflow.com/questions/4697631/android-screen-orientation
【解决方案2】:

我只为纵向模式编写了应用程序。

camera.setDisplayOrientation(90);

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

您必须在下面发送您的活动,cameraId = back 为 0,Front camera 为 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);
} 

这是设置相机的 setDisplayOrientation 的方法,它适用于所有安卓设备。

【讨论】:

  • 适用于显示器,但是当您想要拍照时,您将始终使用前置摄像头旋转 270° 的照片和使用后置摄像头旋转 90° 的照片(如果您有相同的电话asme)此代码只是纠正显示。添加一个camera.getParameters().setRotation(result)就ok了!
  • 我也发现了与@lebarillier 描述的相同的问题。在我的华为Mate 10上,前置摄像头的方向旋转了90。我编码并发布到商店,但在其他设备上发现调整不一样。因此需要着眼于使用传感器,以及计算正确调整的方位角。
猜你喜欢
  • 1970-01-01
  • 2018-06-17
  • 2015-04-12
  • 2013-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多