Skip to content

Commit

Permalink
fix(camera): avoid error if image has no orientation (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Aug 17, 2021
1 parent 5224177 commit dc8a55a
Showing 1 changed file with 7 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,38 +71,15 @@ private static Bitmap transform(final Bitmap bitmap, final Matrix matrix) {
* @return
*/
public static Bitmap correctOrientation(final Context c, final Bitmap bitmap, final Uri imageUri, ExifWrapper exif) throws IOException {
if (Build.VERSION.SDK_INT < 24) {
return correctOrientationOlder(c, bitmap, imageUri);
} else {
final int orientation = getOrientation(c, imageUri);

if (orientation != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
exif.resetOrientation();
return transform(bitmap, matrix);
} else {
return bitmap;
}
}
}

private static Bitmap correctOrientationOlder(final Context c, final Bitmap bitmap, final Uri imageUri) {
// TODO: To be tested on older phone using Android API < 24

String[] orientationColumn = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION };
Cursor cur = c.getContentResolver().query(imageUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Matrix matrix = new Matrix();

if (orientation != -1) {
final int orientation = getOrientation(c, imageUri);
if (orientation != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
exif.resetOrientation();
return transform(bitmap, matrix);
} else {
return bitmap;
}

return transform(bitmap, matrix);
}

private static int getOrientation(final Context c, final Uri imageUri) throws IOException {
Expand Down

0 comments on commit dc8a55a

Please sign in to comment.