Skip to content

Instantly share code, notes, and snippets.

@wangchauyan
Created December 7, 2016 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangchauyan/3b851a446b46b8bbfdf3a7f59def8fc9 to your computer and use it in GitHub Desktop.
Save wangchauyan/3b851a446b46b8bbfdf3a7f59def8fc9 to your computer and use it in GitHub Desktop.
// consider about this url : http://sylvana.net/jpegcrop/exif_orientation.html
// you might consider when you get your image's exif information,
// check the orientation tag, it usually is 1, 3, 6, 8
// So, rotate it.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize, I didn't publish this funciton, but it's easy, just input request width / height
options.inSampleSize = calculateInSampleSize(options, 150, 150);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
// make sure we got correct rotation
ExifInterface exif = new ExifInterface(path);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
}
else if (orientation == 3) {
matrix.postRotate(180);
}
else if (orientation == 8) {
matrix.postRotate(270);
}
bitmap = 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