From 86bb3423f4722569ccb9aafb937cfac8159b65d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Brunk?= Date: Sun, 5 Jan 2025 18:47:07 +0100 Subject: [PATCH] Take into account exif rotation information when scaling images. Fixes #877 --- app/build.gradle | 2 ++ .../patrick/grocy/util/PictureUtil.java | 20 ++++++++++++++++++- gradle/libs.versions.toml | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index a9c34125a..a648a65e3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -127,6 +127,8 @@ dependencies { // ZXing for barcode scanning implementation libs.zxing.core implementation libs.zxing.android.embedded + // For image rotation + implementation libs.exifinterface // https://github.com/journeyapps/zxing-android-embedded#option-2-desugaring-advanced // prevents bug https://github.com/patzly/grocy-android/issues/425 coreLibraryDesugaring libs.desugar diff --git a/app/src/main/java/xyz/zedler/patrick/grocy/util/PictureUtil.java b/app/src/main/java/xyz/zedler/patrick/grocy/util/PictureUtil.java index 9390b18a9..bb5f79e4e 100644 --- a/app/src/main/java/xyz/zedler/patrick/grocy/util/PictureUtil.java +++ b/app/src/main/java/xyz/zedler/patrick/grocy/util/PictureUtil.java @@ -22,11 +22,14 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.Matrix; import android.graphics.drawable.Drawable; +import android.util.Log; import android.view.View; import android.widget.ImageView; import androidx.annotation.Nullable; import androidx.cardview.widget.CardView; +import androidx.exifinterface.media.ExifInterface; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.load.DataSource; @@ -44,6 +47,8 @@ public class PictureUtil { + private static final String TAG = PictureUtil.class.getSimpleName(); + public static void loadPicture(ImageView imageView, @Nullable CardView frame, String pictureUrl) { Glide.with(imageView.getContext()) .load(new GlideUrl( @@ -117,7 +122,20 @@ public static Bitmap scaleBitmap(String imagePath) { int scaleFactor = Math.min(imageWidth / maxWidth, imageHeight / maxHeight); options.inJustDecodeBounds = false; options.inSampleSize = scaleFactor; - return BitmapFactory.decodeFile(imagePath, options); + int rotation = 0; + try { + var exif = new ExifInterface(imagePath); + rotation = exif.getRotationDegrees(); + } catch (IOException e) { + Log.w(TAG, "Reading exif data failed, ignoring possible rotation: " + e); + } + var bitmap = BitmapFactory.decodeFile(imagePath, options); + if (rotation == 0) return bitmap; + else { + var matrix = new Matrix(); + matrix.postRotate(rotation); + return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); + } } public static Bitmap scaleBitmap(Bitmap bitmap) { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index c37dc16f8..ae9cd8184 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -25,6 +25,7 @@ work = "2.10.0" zxing-core = "3.3.0" zxing-android-embedded = "4.3.0" desugar = "2.1.4" +exifinterface = "1.3.7" [libraries] appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } @@ -67,6 +68,7 @@ zxing-android-embedded = { module = "com.journeyapps:zxing-android-embedded", ve # https://github.com/journeyapps/zxing-android-embedded#option-2-desugaring-advanced # prevents bug https://github.com/patzly/grocy-android/issues/425 desugar = { module = "com.android.tools:desugar_jdk_libs", version.ref = "desugar" } +exifinterface = { module = "androidx.exifinterface:exifinterface", version.ref = "exifinterface" } [plugins] android-application = { id = "com.android.application", version.ref = "agp" }