Android摄像头意图保存图像横向拍摄时的肖像 [英] Android Camera Intent Saving Image Landscape When Taken Portrait

查看:172
本文介绍了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.

有没有一种办法,也许检测照片是否摄于肖像模式,然后去$ C C位图$和翻转它的正确方法吗?

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.

下面是一些code,可读取EXIF数据,并相应地旋转图像: 文件是图像文件的名称。

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

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

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