【问题标题】:Camera Intent Image Preview Orientation相机意图图像预览方向
【发布时间】:2019-04-13 21:36:47
【问题描述】:

我使用以下代码在 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);

在相机意图中,我的华为 P20 Pro 上的图像预览始终处于纵向模式。在另一台测试设备上,预览图像(您可以决定是否要重新拍摄图像的图像)也卡在“初始”旋转中,看起来很难看。例如,如果您想以横向模式拍摄图像,则预览会翻转为纵向模式。

有解决办法吗?

【问题讨论】:

    标签: java android android-intent camera


    【解决方案1】:

    大约有 20 亿台 Android 设备,分布在大约 20,000 种设备型号中。这些设备型号中预装了数十个(如果不是数百个)相机应用程序。用户可以下载和安装许多其他相机应用程序。

    您的代码可能会启动其中任何一个。

    在相机意图中,我的华为 P20 Pro 上的图像预览始终处于纵向模式

    这是数百个相机应用程序的行为。

    在另一台测试设备上,预览图像(您可以决定是否要重新拍摄图像)也卡在“初始”旋转中,看起来很难看。

    这是数百个相机应用程序的行为。

    相机应用程序不需要以这种方式运行。当然,相机应用完全不需要预览图像。

    有解决办法吗?

    如果你想使用ACTION_IMAGE_CAPTURE,不。这数百个相机应用的行为取决于这些相机应用的开发者,而不是您。

    还有其他拍照选项,例如直接使用相机 API 或使用 Fotoapparat 或 CameraKit-Android 等第三方库。

    【讨论】:

    • 不过,使用意图可能有充分的理由。最终用户并不关心不同设备和不同相机应用程序之间的不一致行为。毕竟,我们希望用户选择效果最佳的相机应用,并且已经看到与其他使用相机意图的应用相同的丑陋预览屏幕,包括 Google Keep 和其他相当受人尊敬的发布商。
    【解决方案2】:

    使用ExifInterface 在解码时检查图像的方向。然后旋转图像以获得正确方向的所需图像。

            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();
            }
    

    如果您想使用支持库来支持 API 级别较低的设备,请使用以下依赖项:

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

    并导入android.support.media.ExifInterface

    【讨论】:

    • 图像本身在相机意图结束后正确旋转 - 在 Android 相机意图本身中,图片以一种奇怪的方式旋转。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    相关资源
    最近更新 更多