Skip to content

Commit

Permalink
#1293 - HEIC rotation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
budowski committed Aug 18, 2024
1 parent 01ba7dc commit d5afa3b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions iNaturalist/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:installLocation="auto"
android:versionCode="611"
android:versionName="1.31.2">
android:versionCode="612"
android:versionName="1.31.3">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d5afa3b

Please sign in to comment.