简体   繁体   中英

Camera Intent Image Preview Orientation

I take an image in Android using the following Code:

File image = new File(this.images_object_dir, loadedObjekt.getFilename());

Uri uri = FileProvider.getUriForFile(this, FILE_PROVIDER, image);

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, CAMERA_ACTIVITY_CODE);

In the camera intent, the image preview is always in portrait mode on my Huawei P20 Pro. On another test-device the preview image (the one where you can decide if you wanna retake the image) is stuck in the "inital" rotation as well which looks ugly. For instance, if you want to take an image in landscape mode, the preview gets flipped to portrait mode.

Is there a solution for this?

There are ~2 billion Android devices, spread across ~20,000 device models. There are dozens, if not hundreds, of pre-installed camera apps across those device models. There are plenty of other camera apps that the user can download and install.

Your code might start any of them.

In the camera intent, the image preview is always in portrait mode on my Huawei P20 Pro

That is the behavior of that one camera app out of hundreds.

On another test-device the preview image (the one where you can decide if you wanna retake the image) is stuck in the "inital" rotation as well which looks ugly.

That is the behavior of that one camera app out of hundreds.

There is no requirement for a camera app to behave that way. Of course, there is no requirement for a camera app to have preview images at all.

Is there a solution for this?

If you wish to use ACTION_IMAGE_CAPTURE , no. The behavior of those hundreds of camera apps is up to the developers of those camera apps, not you.

There are other options for taking pictures, such as using the camera APIs directly or using third-party libraries like Fotoapparat or CameraKit-Android.

Use ExifInterface to check the orientation of the image while decoding it. Then you rotate the image to get required image in proper orientation.

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inMutable = true;
        Bitmap decoded = BitmapFactory.decodeFile(filePath, options);

        if (decoded == null) return null;

        try {
            ExifInterface exif = new ExifInterface(filePath);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            int rotation = 0;
            if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                rotation = 90;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                rotation = 270;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                rotation = 180;
            }

            if (rotation != 0) {
                Matrix matrix = new Matrix();
                matrix.postRotate(rotation);
                Bitmap newBitmap = Bitmap.createBitmap(decoded, 0, 0, decoded.getWidth(),
                        decoded.getHeight(), matrix, true);
                decoded.recycle();
                Runtime.getRuntime().gc();
                decoded = newBitmap;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

If you want to use support library in order to support devices with lower API levels, use the following dependency:

implementation 'com.android.support:exifinterface:27.1.1'

and import android.support.media.ExifInterface

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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