Skip to content

Commit

Permalink
Take into account exif rotation information when scaling images. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunk committed Jan 5, 2025
1 parent f14ec8b commit 86bb342
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion app/src/main/java/xyz/zedler/patrick/grocy/util/PictureUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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" }
Expand Down

0 comments on commit 86bb342

Please sign in to comment.