Skip to content

Instantly share code, notes, and snippets.

@vitriolix
Forked from T-Spoon/ExifUtil.java
Last active September 11, 2015 22:41
Show Gist options
  • Save vitriolix/5c50439d49ac188c2d31 to your computer and use it in GitHub Desktop.
Save vitriolix/5c50439d49ac188c2d31 to your computer and use it in GitHub Desktop.
Update to ExifUtils.java - Don't use Reflection & use constants instead of magic numbers. Split logic that gets the transposition matrix from the bitmap rotation method so they can each be used independantly
public class ExifUtils {
public static @Nullable Matrix getExifTranspositionMatrix(@NonNull String src) {
Matrix matrix = null;
try {
ExifInterface ei = new ExifInterface(src);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
if (orientation == 1) {
return null;
}
matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
return null;
}
} catch (IOException e) {
e.printStackTrace();
}
return matrix;
}
public static @NonNull Bitmap rotateBitmapForExifData(@NonNull String src, @NonNull Bitmap bitmap) {
Matrix matrix = getExifTranspositionMatrix(src);
if (matrix == null) {
return bitmap;
}
try {
Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return oriented;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return bitmap;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment