Skip to content

Commit 6653871

Browse files
authored
Merge pull request #1 from klinker24/master
Fix rotation on Android, using EXIF data
2 parents 0fe76dd + 8763a26 commit 6653871

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

android/src/main/java/com/terrylinla/rnsketchcanvas/SketchCanvas.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import android.graphics.PointF;
1010
import android.graphics.PorterDuff;
1111
import android.graphics.Rect;
12+
import android.graphics.Matrix;
13+
import android.media.ExifInterface;
1214
import android.os.Environment;
1315
import android.util.Base64;
1416
import android.util.Log;
@@ -69,10 +71,30 @@ public boolean openImageFile(String filename, String directory, String mode) {
6971
"drawable",
7072
mContext.getPackageName());
7173
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
74+
File file = new File(filename, directory == null ? "" : directory);
7275
Bitmap bitmap = res == 0 ?
73-
BitmapFactory.decodeFile(new File(filename, directory == null ? "" : directory).toString(), bitmapOptions) :
76+
BitmapFactory.decodeFile(file.toString(), bitmapOptions) :
7477
BitmapFactory.decodeResource(mContext.getResources(), res);
75-
if(bitmap != null) {
78+
79+
try {
80+
ExifInterface exif = new ExifInterface(file.getAbsolutePath());
81+
Matrix matrix = new Matrix();
82+
83+
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
84+
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
85+
matrix.postRotate(90);
86+
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
87+
matrix.postRotate(180);
88+
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
89+
matrix.postRotate(270);
90+
}
91+
92+
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); // rotating bitmap
93+
} catch (Exception e) {
94+
95+
}
96+
97+
if (bitmap != null) {
7698
mBackgroundImage = bitmap;
7799
mOriginalHeight = bitmap.getHeight();
78100
mOriginalWidth = bitmap.getWidth();

0 commit comments

Comments
 (0)