Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓照相/摄像机拍摄人像时保存图像风景_Android_Android Intent_Android Camera_Android Camera Intent - Fatal编程技术网

Android 安卓照相/摄像机拍摄人像时保存图像风景

Android 安卓照相/摄像机拍摄人像时保存图像风景,android,android-intent,android-camera,android-camera-intent,Android,Android Intent,Android Camera,Android Camera Intent,我环顾四周,但似乎没有一个可靠的答案/解决方案来解决这个非常令人恼火的问题 我以纵向拍摄照片,当我点击保存/放弃按钮时,按钮也处于正确的方向。问题是当我稍后检索图像时,它是横向的(图片已逆时针旋转90度) 我不想强迫用户以特定的方向使用相机 是否有一种方法可以检测照片是否是在纵向模式下拍摄的,然后解码位图并将其向上翻转?照片始终以相机内置在设备中的方向拍摄。要使图像正确旋转,必须读取存储在图片中的方向信息(EXIF元数据)。它存储了拍摄图像时设备的方向 下面是一些读取EXIF数据并相应旋转图像的

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

我以纵向拍摄照片,当我点击保存/放弃按钮时,按钮也处于正确的方向。问题是当我稍后检索图像时,它是横向的(图片已逆时针旋转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支持库的发布,引入了ExiFinInterface支持库,这可能会使访问Exif属性更加容易。有关这方面的文章,请参阅。

选定的答案使用了回答此问题和类似问题的最常用方法。然而,三星的前后摄像头对我都不起作用。对于那些需要另一种解决方案的用户,该解决方案适用于三星和其他主要制造商的前置和后置摄像头,nvhausid的回答非常棒:

对于那些不想点击的人来说,相关的魔法是使用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);

链接中的完整代码。

+1非常感谢!如果不(有时)创建第二个位图,就没有办法。该死的安卓和它的小虚拟机你在哪里初始化
选项
调整大小的位图
?@lschessinger,好捕获(2年后)。我已经更新了代码。请看@SrujanBarai我不这么认为:它不太管用……….
mcurrentcomeraid
来自哪里?