Skip to content

Commit

Permalink
fix(android): Properly reset orientation exif if corrected (#4912)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Aug 12, 2021
1 parent 609dfe1 commit 214b744
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,13 @@ private Uri saveTemporaryImage(Bitmap bitmap, Uri contentUri, InputStream is) th
* @param u
*/
private void returnResult(PluginCall call, Bitmap bitmap, Uri u) {
ExifWrapper exif = ImageUtils.getExifData(getContext(), bitmap, u);
try {
bitmap = prepareBitmap(bitmap, u);
bitmap = prepareBitmap(bitmap, u, exif);
} catch (IOException e) {
call.reject(UNABLE_TO_PROCESS_IMAGE);
return;
}

ExifWrapper exif = ImageUtils.getExifData(getContext(), bitmap, u);

// Compress the final image and prepare for output to client
ByteArrayOutputStream bitmapOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, settings.getQuality(), bitmapOutputStream);
Expand Down Expand Up @@ -404,11 +402,12 @@ private Uri getTempImage(Bitmap bitmap, Uri u, ByteArrayOutputStream bitmapOutpu
* recycling the old one in the process
* @param bitmap
* @param imageUri
* @param exif
* @return
*/
private Bitmap prepareBitmap(Bitmap bitmap, Uri imageUri) throws IOException {
private Bitmap prepareBitmap(Bitmap bitmap, Uri imageUri, ExifWrapper exif) throws IOException {
if (settings.isShouldCorrectOrientation()) {
final Bitmap newBitmap = ImageUtils.correctOrientation(getContext(), bitmap, imageUri);
final Bitmap newBitmap = ImageUtils.correctOrientation(getContext(), bitmap, imageUri, exif);
bitmap = replaceBitmap(bitmap, newBitmap);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,8 @@ public void copyExif(String destFile) {
destExif.saveAttributes();
} catch (Exception ex) {}
}

public void resetOrientation() {
exif.resetOrientation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ private static Bitmap transform(final Bitmap bitmap, final Matrix matrix) {
* the appropriate amount for portrait mode
* @param bitmap
* @param imageUri
* @param exif
* @return
*/
public static Bitmap correctOrientation(final Context c, final Bitmap bitmap, final Uri imageUri) throws IOException {
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 {
Expand All @@ -117,9 +118,7 @@ public static Bitmap correctOrientation(final Context c, final Bitmap bitmap, fi
if (orientation != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
ExifInterface exif = new ExifInterface(imageUri.getPath());
exif.resetOrientation();
exif.saveAttributes();
return transform(bitmap, matrix);
} else {
return bitmap;
Expand Down

0 comments on commit 214b744

Please sign in to comment.