-
-
Save MacKentoch/04d8015884b89c2d30f03b0ab4a7fad9 to your computer and use it in GitHub Desktop.
Android code to rotate an image based on exif information
This file contains hidden or 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
// 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