如何从Android摄像头图像中剥离EXIF数据
我需要在我的应用程序中将照片从相机上传到服务器。它在大多数设备上都非常有效。但一些设备导致图像旋转90’,这不是我想要的行为。经过研究,我了解到这是由于EXIF数据附有图像。为了从图像位图中去除EXIF数据,我尝试了各种方法,如重新调整图像大小等,但没有一种方法对我有效。任何人都可以提出执行此任务的方法。试试这个如何从Android摄像头图像中剥离EXIF数据,android,android-camera,android-bitmap,Android,Android Camera,Android Bitmap,我需要在我的应用程序中将照片从相机上传到服务器。它在大多数设备上都非常有效。但一些设备导致图像旋转90’,这不是我想要的行为。经过研究,我了解到这是由于EXIF数据附有图像。为了从图像位图中去除EXIF数据,我尝试了各种方法,如重新调整图像大小等,但没有一种方法对我有效。任何人都可以提出执行此任务的方法。试试这个 public static Bitmap getImage(Context context, Uri uri) throws FileNotFoundExcep
public static Bitmap getImage(Context context, Uri uri)
throws FileNotFoundException, IOException {
InputStream input = context.getContentResolver().openInputStream(uri);
BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
onlyBoundsOptions.inJustDecodeBounds = true;
onlyBoundsOptions.inDither = true;// optional
onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
input.close();
if ((onlyBoundsOptions.outWidth == -1)
|| (onlyBoundsOptions.outHeight == -1))
return null;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inDither = true;
bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
input = context.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
input.close();
ExifInterface ei = new ExifInterface(uri.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
bitmap = rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
bitmap = rotateImage(bitmap, 180);
break;
}
return bitmap;
}
这里的uri是从相机拍摄的图像的uri
为了理解exif定向:转到这里,我们使用ExiFinInterface对象读取JPEG文件中的标签并获取图像的定向属性,例如
// Variable to store the corrected bitmap.
Bitmap correctedBitMap = null;
ExifInterface exifInterface = new ExifInterface(<PATH OF YOUR PHOTO>);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
位图是从哪里来的?但是你的代码做什么呢?总是轮换吗?请澄清。它实际上执行两项任务:Ist是从uri获取位图,直到
输入。关闭()
之后,它将从图像获取方向标记,如果它被摄像机旋转,那么它将旋转。但最初的请求不是旋转。是,仅当位图被有意旋转时才旋转位图(由于方向改变或类似galaxy s3的摄像头硬件,请参见)不是每次都由摄像头旋转。感谢您的努力。实际上我已经在请求URI
加载位图。问题是,应用程序中摄像头拍摄的照片是临时存储的。因此,当我再次请求URI
时,会导致崩溃。还有其他建议吗?有没有办法在位图上执行此功能对象?为此,您可以查看我的答案。这里检查最后一段代码。根据您的要求,您是否能够从图像中剥离EXIF数据?这里给出的解决方案会旋转图像。
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
correctedBitMap = rotateImage(<YOUR BITMAP OBJECT>, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
correctedBitMap = rotateImage(<YOUR BITMAP OBJECT>, 180);
break;
}
private Bitmap rotateImage(Bitmap source, float angle) {
Bitmap bitmap = null;
Matrix matrix = new Matrix();
matrix.postRotate(angle);
try {
bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
return bitmap;
}