Android相机意图在拍摄人像时保存图像风景 [英] Android Camera Intent Saving Image Landscape When Taken Portrait

查看:28
本文介绍了Android相机意图在拍摄人像时保存图像风景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,但似乎没有一个可靠的答案/解决方案来解决这个非常令人恼火的问题.

I have had a look around but there doesn't seem to be a solid answer/solution to the, very irritating, problem.

我以纵向拍摄照片,当我点击保存/丢弃按钮时,按钮的方向也正确.问题是当我稍后检索图像时,它是横向的(图片已逆时针旋转 90 度)

I take a picture in portrait orientation and when I hit save/discard the buttons are in the correct orientation also. The problem is when I then retrieve the image later on it is in landscape orientation (the picture has been rotated 90 degrees anti-clockwise)

我不想强迫用户在某个方向使用相机.

I don' want to force the user to use the camera in a certain orientation.

有没有办法检测照片是否是在人像模式下拍摄的,然后解码位图并以正确的方式向上翻转?

Is there a way to maybe detect whether the photo was taken in portrait mode and then decode the bitmap and flip it the correct way up?

推荐答案

照片总是以相机内置于设备的方向拍摄.要正确旋转图像,您必须读取存储在图片中的方向信息(EXIF 元数据).那里存储了设备的方向,以及拍摄图像的时间.

The picture is always taken in the orientation the camera is built into the device. To get your image rotated correctly you'll have to read the orientation information that is stored into the picture (EXIF meta data). There it is stored how the device was oriented, when the image was taken.

这是一些读取 EXIF 数据并相应地旋转图像的代码:file 是图像文件的名称.

Here is some code that reads the EXIF data and rotates the image accordingly: file is the name of the image file.

BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bounds);

BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(file, opts);
ExifInterface exif = new ExifInterface(file);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer.parseInt(orientString) :  ExifInterface.ORIENTATION_NORMAL;

int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;

Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);

更新 2017-01-16

随着 25.1.0 支持库的发布,引入了 ExifInterface 支持库,这可能会使访问 Exif 属性更容易.请参阅 Android 开发者博客,了解关于它的文章.

With the release of the 25.1.0 Support Library, an ExifInterface Support Library was introduced, which should perhaps make the access to the Exif attributes easier. See the Android Developer's Blog for an article about it.

这篇关于Android相机意图在拍摄人像时保存图像风景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆