【问题标题】:Camera preview is upside down相机预览是颠倒的
【发布时间】:2016-05-17 17:12:34
【问题描述】:

我有一个使用安卓设备摄像头的应用程序。拍照的activity不能旋转,只能纵向显示。 在大多数设备上,此代码都可以正常工作:

int degrees = 0;
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;
         if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
             result = (cameraInfo.orientation + degrees) % 360;
             result = (360 - result) % 360;  // compensate the mirror
         } else {  // back-facing
             result = (cameraInfo.orientation - degrees + 360) % 360;
         } camera.setDisplayOrientation(result);

但在设备(DMTECH 725H,7 英寸平板电脑,只有前置摄像头)上,预览显示是颠倒的。有任何解决方法的想法吗?

【问题讨论】:

  • 此设备是否正确将cameraInfo.facing 设置为CAMERA_FACING_FRONT,还是说相机朝后?
  • 我不知道,我无法在设备上调试应用程序,但我尝试将相机旋转 90 度和 270 度,结果是一样的
  • 我在 Nexus 5X (6.0.1) 中遇到了同样的问题

标签: android android-camera


【解决方案1】:

试试这个:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    boolean isLandscape = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE;

    int degrees = 0;
    switch (mDisplay.getRotation()){
        case Surface.ROTATION_0:
            degrees = isLandscape? 0 : 90;
            break;
        case Surface.ROTATION_90:
            degrees = isLandscape? 0 : 270;
            break;
        case Surface.ROTATION_180:
            degrees = isLandscape? 180 : 270;
            break;
        case Surface.ROTATION_270:
            degrees = isLandscape? 180 : 90;
            break;
    }
    cameraManager.rotateDisplay(degrees, isLandscape);
}

【讨论】:

  • 谢谢,但这个解决方案对我不起作用,因为我的方向永远不会是横向的。
  • 你写的,但如果你没有在AndroidManifest.xml中设置活动的属性android:screenOrientation="nosensor",你必须管理横向并相应地改变度数。
【解决方案2】:

this support issue 中所述,相机旋转错误的原因可能是传感器方向异常。您还应该使用camera2 包而不是android.hardware.Camera

【讨论】:

    【解决方案3】:

    这可能不是完美的解决方案。但是请参考它,希望它对你有用:)

    android.hardware.Camera.setDisplayOrientation

    http://developer.android.com/reference/android/hardware/Camera.Parameters.html

    android.hardware.Camera.Parameters.setRotation

    http://developer.android.com/reference/android/hardware/Camera.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      • 1970-01-01
      • 2020-11-21
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多