-
-
Save T-Spoon/56e0f6190c425b71d457 to your computer and use it in GitHub Desktop.
Update to ExifUtils.java - Don't use Reflection & use constants instead of magic numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExifUtils { | |
public static Bitmap rotateBitmapForExifData(String src, Bitmap bitmap) { | |
try { | |
ExifInterface ei = new ExifInterface(src); | |
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); | |
if (orientation == 1) { | |
return bitmap; | |
} | |
Matrix 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 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; | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return bitmap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment