拍摄肖像时,Android相机意图保存图像风景

Stu*_*ing 37 android android-intent android-camera android-camera-intent

我看了一下,但似乎没有一个坚实的答案/解决方案,非常恼人的问题.

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

我不想强迫用户以特定方向使用相机.

有没有办法可以检测照片是以纵向模式拍摄,然后解码位图并以正确的方式翻转它?

Rid*_*lly 87

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

以下是一些读取EXIF数据并相应旋转图像的代码: 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);
Run Code Online (Sandbox Code Playgroud)

更新2017-01-16

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

  • +1非常感谢!没有(有时)创建第二个位图,没有办法.该死的Android及其微小的虚拟机 (11认同)
  • @lschessinger,好抓(2年后).我已经更新了代码. (2认同)