Skip to content

Instantly share code, notes, and snippets.

@vbuberen
Last active September 4, 2018 12:34
Show Gist options
  • Save vbuberen/fbb8b973aa49c9e81b1d0d8ffd8bc506 to your computer and use it in GitHub Desktop.
Save vbuberen/fbb8b973aa49c9e81b1d0d8ffd8bc506 to your computer and use it in GitHub Desktop.
Image rotating method for restoring correct image rotations on devices with physically rotated camera
private Bitmap restoreRotation(File originalFile) throws IOException {
int rotationAngle;
ExifInterface ei = new ExifInterface(Uri.fromFile(originalFile).getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotationAngle = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotationAngle = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotationAngle = 270;
break;
default:
rotationAngle = 0;
}
Bitmap imageToRotate = BitmapFactory.decodeFile(originalFile.getAbsolutePath());
Matrix matrix = new Matrix();
matrix.postRotate(rotationAngle);
Bitmap rotatedBitmap = Bitmap.createBitmap(imageToRotate, 0, 0, imageToRotate.getWidth(), imageToRotate.getHeight(), matrix, true);
return rotatedBitmap;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment