ExifInterface 始终 return 0 方向
ExifInterface always return 0 Orientation
我看到很多关于此的问题,但我没有找到问题的答案。
我尝试显示一张可以用相机拍摄或从存储中加载的个人资料图像。
我使用 ExifInterface 来确定加载图像的 Picasso 的正确旋转。
我不明白为什么所有图片的方向 = 0
下面是我的代码,非常简单:
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
Picasso.with(getBaseContext()).load("file:" + destination.getPath()).rotate(MyTools.getFileExifRotation("file:" + destination.getPath())).into(avatar);
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
Uri uri=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
uri=data.getData();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Picasso.with(getBaseContext()).load("file:" + uri.getPath()).rotate(MyTools.getFileExifRotation("file:" + uri.getPath())).into(avatar);
} catch (IOException e) {
e.printStackTrace();
}
}
public static int getFileExifRotation(String path) throws IOException {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return 90;
case ExifInterface.ORIENTATION_ROTATE_180:
return 180;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270;
default:
return 0;
}
}
我在 LG G4 上测试 phone。
你有一张位图。位图不包含方向。之后,您将位图压缩为 .jpg。这不会添加方向或 exif header。
所以那个.jpg用ExifInterface是没有用的。或者尝试获取方向。
你知道你在保存缩略图吗!?改用原来的。
我看到很多关于此的问题,但我没有找到问题的答案。
我尝试显示一张可以用相机拍摄或从存储中加载的个人资料图像。
我使用 ExifInterface 来确定加载图像的 Picasso 的正确旋转。
我不明白为什么所有图片的方向 = 0
下面是我的代码,非常简单:
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
Picasso.with(getBaseContext()).load("file:" + destination.getPath()).rotate(MyTools.getFileExifRotation("file:" + destination.getPath())).into(avatar);
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
Uri uri=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
uri=data.getData();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Picasso.with(getBaseContext()).load("file:" + uri.getPath()).rotate(MyTools.getFileExifRotation("file:" + uri.getPath())).into(avatar);
} catch (IOException e) {
e.printStackTrace();
}
}
public static int getFileExifRotation(String path) throws IOException {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return 90;
case ExifInterface.ORIENTATION_ROTATE_180:
return 180;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270;
default:
return 0;
}
}
我在 LG G4 上测试 phone。
你有一张位图。位图不包含方向。之后,您将位图压缩为 .jpg。这不会添加方向或 exif header。
所以那个.jpg用ExifInterface是没有用的。或者尝试获取方向。
你知道你在保存缩略图吗!?改用原来的。