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

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

我以纵向拍摄照片,当我点击保存/放弃button也是在正确的方向。 问题是当我然后检索图像是横向(图片已被逆时针旋转了90度)

我不想强迫用户以某种方向使用相机。

有没有一种方法可能检测照片是否采取肖像模式,然后解码位图并翻转它正确的方式了吗?

照片始终以相机内置于设备中的方向拍摄。 要正确旋转图像,您必须阅读存储在图片中的方向信息(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); 

更新2017-01-16

随着25.1.0支持库的发布,ExifInterface支持库被引入,这可能使Exif属性的访问更容易。 有关这方面的文章,请参阅Android Developer's Blog 。

选定的答案使用最常见的方法回答这个和类似的问题。 但是,在三星的前置和后置摄像头都不适合我。 对于那些需要另一种解决scheme,适用于三星和其他主要制造商的正面和背面相机,这个由nvhausid的答案是真棒:

https://stackoverflow.com/a/18915443/6080472

对于那些不想点击的人来说,相关的魔术就是使用CameraInfo而不是依靠EXIF或光标来播放媒体文件。

 Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length); android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); android.hardware.Camera.getCameraInfo(mCurrentCameraId, info); Bitmap bitmap = rotate(realImage, info.orientation); 

完整的代码在链接中。