为了账号安全,请及时绑定邮箱和手机立即绑定

相机意图图像预览方向

相机意图图像预览方向

森林海 2022-01-12 09:57:44
我使用以下代码在 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 上的图像预览始终处于纵向模式。在另一台测试设备上,预览图像(您可以决定是否要重新拍摄图像的图像)也卡在“初始”旋转中,看起来很难看。例如,如果您想以横向模式拍摄图像,则预览将翻转为纵向模式。有解决方案吗?
查看完整描述

2 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

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

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

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

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

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

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

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

有解决方案吗?

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

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


查看完整回答
反对 回复 2022-01-12
?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

用于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


查看完整回答
反对 回复 2022-01-12
  • 2 回答
  • 0 关注
  • 194 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号