From d5afa3b2520c3e584ec6e1489c13ab276616ed2b Mon Sep 17 00:00:00 2001 From: Yaron Budowski Date: Sun, 18 Aug 2024 17:16:39 +0200 Subject: [PATCH] #1293 - HEIC rotation fix --- iNaturalist/src/main/AndroidManifest.xml | 4 ++-- .../org/inaturalist/android/ImageUtils.java | 22 ++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/iNaturalist/src/main/AndroidManifest.xml b/iNaturalist/src/main/AndroidManifest.xml index 1e2faa703..be31d2d40 100644 --- a/iNaturalist/src/main/AndroidManifest.xml +++ b/iNaturalist/src/main/AndroidManifest.xml @@ -2,8 +2,8 @@ + android:versionCode="612" + android:versionName="1.31.3"> diff --git a/iNaturalist/src/main/java/org/inaturalist/android/ImageUtils.java b/iNaturalist/src/main/java/org/inaturalist/android/ImageUtils.java index efcf07c86..27637774c 100644 --- a/iNaturalist/src/main/java/org/inaturalist/android/ImageUtils.java +++ b/iNaturalist/src/main/java/org/inaturalist/android/ImageUtils.java @@ -327,6 +327,14 @@ public static int getImageOrientation(String imgFilePath) { return degrees; } catch (Exception e) { Logger.tag(TAG).error(e); + + try { + androidx.exifinterface.media.ExifInterface orgExif = new androidx.exifinterface.media.ExifInterface(imgFilePath); + return orgExif.getRotationDegrees(); + } catch (IOException ex) { + Logger.tag(TAG).error(e); + } + // No orientation return 0; } @@ -349,6 +357,10 @@ private static int exifOrientationToDegrees(int orientation) { public static Bitmap rotateAccordingToOrientation(Bitmap bitmapImage, String filename) { int orientation = getImageOrientation(filename); + return rotateImage(bitmapImage, orientation); + } + + public static Bitmap rotateImage(Bitmap bitmapImage, int orientation) { if (orientation != 0) { // Rotate the image Matrix matrix = new Matrix(); @@ -360,6 +372,7 @@ public static Bitmap rotateAccordingToOrientation(Bitmap bitmapImage, String fil } + public static String resizeImage(Context context, String path, Uri photoUri, int maxDimensions) { return resizeImage(context, path, photoUri, maxDimensions, false); } @@ -400,12 +413,16 @@ public static String resizeImage(Context context, String path, Uri photoUri, int // BitmapFactory.decodeStream moves the reading cursor is.close(); + androidx.exifinterface.media.ExifInterface exif = new androidx.exifinterface.media.ExifInterface(path); + int rotationDegrees = exif.getRotationDegrees(); + if (photoUri != null) { is = context.getContentResolver().openInputStream(photoUri); } else { is = new FileInputStream(new File(path)); } + if (Math.max(originalHeight, originalWidth) < maxDimensions) { // Original file is smaller than max // Don't resize because image is smaller than max - however, make a local copy of it @@ -450,16 +467,19 @@ public static String resizeImage(Context context, String path, Uri photoUri, int return null; } + Bitmap rotatedBitmap = rotateImage(resizedBitmap, rotationDegrees); + // Save resized image File imageFile = new File(context.getFilesDir(), UUID.randomUUID().toString() + ".jpeg"); OutputStream os = new FileOutputStream(imageFile); - resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); + rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); os.flush(); os.close(); Logger.tag(TAG).debug(String.format("resizeImage: %s => %s", path, imageFile.getAbsolutePath())); resizedBitmap.recycle(); + rotatedBitmap.recycle(); // BitmapFactory.decodeStream moves the reading cursor is.close();