Skip to content

Commit

Permalink
Fix rotation on Android, using EXIF data
Browse files Browse the repository at this point in the history
Some Android phones have their camera sensors mounted sideways, upside down, ect. The BitmapFactory#decodeFile method does not take this into account, by default. So, when the file is decoded, it could end up getting shown in an incorrect orientation on the canvas.

iOS manages this correctly already. This PR just fixes the issue for Android devices.

Please refer original commit to root repo terrylinla#135
  • Loading branch information
maheshwaripurvesh authored Sep 19, 2022
1 parent d8a6554 commit c8d1c34
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
Expand Down Expand Up @@ -56,9 +58,29 @@ public boolean openImageFile(String filename, String directory, String mode) {
"drawable",
mContext.getPackageName());
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
File file = new File(filename, directory == null ? "" : directory);
Bitmap bitmap = res == 0 ?
BitmapFactory.decodeFile(new File(filename, directory == null ? "" : directory).toString(), bitmapOptions) :
BitmapFactory.decodeFile(file).toString(), bitmapOptions) :
BitmapFactory.decodeResource(mContext.getResources(), res);

try {
ExifInterface exif = new ExifInterface(file.getAbsolutePath());
Matrix matrix = new Matrix();

int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
matrix.postRotate(90);
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
matrix.postRotate(180);
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
matrix.postRotate(270);
}

bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); // rotating bitmap
} catch (Exception e) {

}

if(bitmap != null) {
mBackgroundImage = bitmap;
mOriginalHeight = bitmap.getHeight();
Expand Down

0 comments on commit c8d1c34

Please sign in to comment.