【问题标题】:Rotating device changing image orientation ?旋转设备改变图像方向?
【发布时间】:2013-08-30 07:29:22
【问题描述】:

这是我的相机活动,它处于纵向模式。我正在旋转设备,相机也随之旋转。

这是我的第二个Activity,我的图像在ImageView 上颠倒显示,来自PictureCallback()。我已经尝试过ExifInterface,但它的返回方向为 0。

【问题讨论】:

    标签: android android-camera android-orientation


    【解决方案1】:

    我遇到了类似的问题。 ExifInterface 中的方向 0 表示未定义。如果发生这种情况,我会询问 MediaStore,在我的情况下,如果 ExifInterface 返回 0,它总是会返回正确的方向。

    String[] cols = { MediaStore.Images.Media.ORIENTATION };
    Cursor cur = context.getContentResolver().query(uri, cols, null, null, null);
    int orientation = 0;
    
    if (cur != null && cur.moveToFirst()) {
            orientation = cur.getInt(cur.getColumnIndex(MediaStore.Images.Media.ORIENTATION));
    }
    

    【讨论】:

    • 仍然获得方向 = 0;
    【解决方案2】:

    相机设置:

    public static void setCameraDisplayOrientation(Activity activity,
                int cameraId, android.hardware.Camera camera) {
            try {
    
                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;
                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);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    

    并在显示结果时使用ExifInterface

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多