在我的 Android 应用程序中,我正在从设备库加载图像。在这方面,我面临着有关图像方向的问题。当我从图库加载高分辨率图像时,它们会自动旋转然后显示在我的视图中。我尝试了有关此问题的各种解决方案,但无法得到正确的解决方案。我提到获取方向() https://stackoverflow.com/questions/12369138/disable-android-image-auto-rotate , and this https://stackoverflow.com/questions/3647993/android-bitmaps-loaded-from-gallery-are-rotated-in-imageview链接。我已经尝试了两种解决方案,但无法得到想要的结果。ExifInterface
返回正确的数据,但它们也没有帮助,因为图像旋转是因为它们的高分辨率而不是因为相机方向。请帮我解决这个问题。
谢谢。
这很有效;
public class ExifUtils {
public static Bitmap getRotatedBitmap(Context context, Uri uri) {
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
float width = (float) bitmap.getWidth();
float height = (float) bitmap.getHeight();
float max = Math.max(width / 1280.0f, height / 1280.0f);
if (max > 1.0f) {
bitmap = Bitmap.createScaledBitmap(bitmap, (int) (width / max), (int) (height / max), false);
}
Bitmap rotateBitmap = rotateBitmap(bitmap,
new ExifInterface(context.getContentResolver().openInputStream(uri))
.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1));
if (rotateBitmap != bitmap) {
bitmap.recycle();
}
return rotateBitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static Bitmap rotateBitmap(Bitmap bitmap, int i) {
Matrix matrix = new Matrix();
switch (i) {
case 2:
matrix.setScale(-1.0f, 1.0f);
break;
case 3:
matrix.setRotate(180.0f);
break;
case 4:
matrix.setRotate(180.0f);
matrix.postScale(-1.0f, 1.0f);
break;
case 5:
matrix.setRotate(90.0f);
matrix.postScale(-1.0f, 1.0f);
break;
case 6:
matrix.setRotate(90.0f);
break;
case 7:
matrix.setRotate(-90.0f);
matrix.postScale(-1.0f, 1.0f);
break;
case 8:
matrix.setRotate(-90.0f);
break;
default:
return bitmap;
}
try {
Bitmap createBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
bitmap.recycle();
return createBitmap;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)