Skip to content

Instantly share code, notes, and snippets.

@valentincognito
Last active July 26, 2018 03:07
Show Gist options
  • Save valentincognito/90e353571d4745bf7934f17e7924b7b8 to your computer and use it in GitHub Desktop.
Save valentincognito/90e353571d4745bf7934f17e7924b7b8 to your computer and use it in GitHub Desktop.
Fix the stupid SAMSUNG rotation of picture taken with the camera
File imgFile = new File(image_path); //INTERNAL STORAGE path
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Uri internal = Uri.fromFile(imgFile);
try{
Bitmap img = rotateImageIfRequired(myBitmap, internal);
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* Rotate an image if required.
*
* @param img The image bitmap
* @param selectedImage Image URI
* @return The resulted Bitmap after manipulation
*/
private static Bitmap rotateImageIfRequired(Bitmap img, Uri selectedImage) throws IOException {
ExifInterface ei = new ExifInterface(selectedImage.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotateImage(img, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotateImage(img, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotateImage(img, 270);
default:
return img;
}
}
private static Bitmap rotateImage(Bitmap img, int degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
//img.recycle();
return rotatedImg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment