Skip to content

Instantly share code, notes, and snippets.

@yhirano
Created October 20, 2017 10:21
Show Gist options
  • Select an option

  • Save yhirano/2fbc6fcc07ff8799d0d71ad8895ca5ea to your computer and use it in GitHub Desktop.

Select an option

Save yhirano/2fbc6fcc07ff8799d0d71ad8895ca5ea to your computer and use it in GitHub Desktop.
Android Exif rotation.
private Bitmap getBitmap(@NonNull File file) {
int exifRotation = getExifRotation(file);
if (exifRotation != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(exifRotation);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
if (!bitmap.isRecycled()) {
bitmap.recycle();
}
bitmap = rotatedBitmap;
}
retirm bitmap
}
private static int getExifRotation(@NonNull File imageFile) {
if (imageFile == null) return 0;
try {
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)) {
case ExifInterface.ORIENTATION_ROTATE_90:
return 90;
case ExifInterface.ORIENTATION_ROTATE_180:
return 180;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270;
default:
return 0;
}
} catch (IOException e) {
Log.e(TAG, "Error getting Exif data", e);
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment