繁体   English   中英

从相机/图库加载图像位图后会旋转[Android 9]

[英]Image Bitmap gets rotated when loaded from camera/gallery [Android 9]

我正在从图库或照相机中加载图像,以便可以对其进行编辑。 我已经使用EXIFInterface来管理位图的旋转。 但是在Samsung s8 [Android 9]中,它为图像提供了90度旋转,但是图像最初并未旋转。 并根据此旋转将其旋转90度,这是我不希望的。

我尝试使用ContentResolver通过光标进行旋转,但是它也存在与EXIFInterface相同的问题。 以下是我尝试解决问题的两种方法:

private static int getExifOrientation(String image_absolute_path) throws IOException {
    ExifInterface ei = new ExifInterface(image_absolute_path);
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
                return RotationOptions.ROTATE_90;
            case ExifInterface.ORIENTATION_ROTATE_180:
                return RotationOptions.ROTATE_180;
            case ExifInterface.ORIENTATION_ROTATE_270:
                return RotationOptions.ROTATE_270;
            default:
                return RotationOptions.NO_ROTATION;
    }
}

 private static int getOrientation(Context context, Uri photoUri) {
        try {
            Uri imageContentUri = getImageContentUri(context, photoUri.getPath());
            if (imageContentUri == null) {
                return -1;
            }
            Cursor cursor = context.getContentResolver().query(imageContentUri, new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, null, null, null);
            if (cursor == null) {
                return -1;
            }
            if (cursor.getCount() != 1) {
                cursor.close();
                return -1;
            }
            cursor.moveToFirst();
            int orientation = cursor.getInt(0);
            cursor.close();
            cursor = null;
            return orientation;
        } catch (Exception e) {
            return -1;
        }
    }

我也面临着同样的问题。 我在没有EXIFInterface的情况下进行管理,

通过在这种情况下旋转图像-

if (bm.getWidth() > bm.getHeight()) {
            bm = rotateImage(bm, 270);
        }
        bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM