Skip to content

Instantly share code, notes, and snippets.

@MacKentoch
Forked from tomogoma/ImageRotator.java
Created November 13, 2019 08:59
Show Gist options
  • Save MacKentoch/04d8015884b89c2d30f03b0ab4a7fad9 to your computer and use it in GitHub Desktop.
Save MacKentoch/04d8015884b89c2d30f03b0ab4a7fad9 to your computer and use it in GitHub Desktop.
Android code to rotate an image based on exif information
// imports
public class ImageRotator {
public static Bitmap rotateImage(String path) throws IOException {
Bitmap bitmap = BitmapFactory.decodeFile(path);
return rotateImage(bitmap);
}
public static Bitmap rotateImage(Bitmap bitmap) throws IOException {
int rotate = 0;
ExifInterface exif;
exif = new ExifInterface(path);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment