简体   繁体   English

相机意图图像预览方向

[英]Camera Intent Image Preview Orientation

I take an image in Android using the following Code: 我使用以下代码在Android中拍摄图像:

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. 按照相机的意图,图像预览始终在我的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. 大约有20亿个Android设备,分布在约20,000个设备模型中。 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 在相机意图下,我的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. 如果您想使用ACTION_IMAGE_CAPTURE ,则不会。 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. 还有其他拍照选项,例如直接使用相机API或使用第三方库(例如Fotoapparat或CameraKit-Android)。

Use ExifInterface to check the orientation of the image while decoding it. ExifInterface ,请使用ExifInterface检查图像的方向。 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: 如果要使用支持库来支持具有较低API级别的设备,请使用以下依赖项:

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

and import android.support.media.ExifInterface 并导入android.support.media.ExifInterface

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

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