@@ -114,7 +114,44 @@ public static Bitmap decodeSampledBitmapFromFile(String srcPath, int reqWidth, i
114
114
return BitmapFactory .decodeFile (srcPath , options );
115
115
}
116
116
117
+ /**
118
+ * Decodes a bitmap from a file containing it minimizing the memory use. Scales image to screen size.
119
+ *
120
+ * @param storagePath Absolute path to the file containing the image.
121
+ */
122
+ public static Bitmap retrieveBitmapFromFile (String storagePath , int minWidth , int minHeight ){
123
+ // Get the original dimensions of the bitmap
124
+ var bitmapResolution = getImageResolution (storagePath );
125
+ var originalWidth = bitmapResolution [0 ];
126
+ var originalHeight = bitmapResolution [1 ];
127
+
128
+ // Detect Orientation and swap height/width if the image is to be rotated
129
+ var shouldRotate = detectRotateImage (storagePath );
130
+ if (shouldRotate ) {
131
+ // Swap the width and height
132
+ var tempWidth = originalWidth ;
133
+ originalWidth = originalHeight ;
134
+ originalHeight = tempWidth ;
135
+ }
136
+
137
+ var bitmapResult = decodeSampledBitmapFromFile (
138
+ storagePath , originalWidth , originalHeight );
139
+
140
+ // Calculate the scaling factors based on screen dimensions
141
+ var widthScaleFactor = (float ) minWidth / bitmapResult .getWidth ();
142
+ var heightScaleFactor = (float ) minHeight / bitmapResult .getHeight ();
143
+
144
+ // Use the smaller scaling factor to maintain aspect ratio
145
+ var scaleFactor = Math .min (widthScaleFactor , heightScaleFactor );
117
146
147
+ // Calculate the new scaled width and height
148
+ var scaledWidth = (int ) (bitmapResult .getWidth () * scaleFactor );
149
+ var scaledHeight = (int ) (bitmapResult .getHeight () * scaleFactor );
150
+
151
+ bitmapResult = scaleBitmap (bitmapResult ,scaledWidth ,scaledHeight );
152
+
153
+ return bitmapResult ;
154
+ }
118
155
/**
119
156
* Calculates a proper value for options.inSampleSize in order to decode a Bitmap minimizing the memory overload and
120
157
* covering a target surface of reqWidth x reqHeight if the original image is big enough.
@@ -162,6 +199,18 @@ public static Bitmap scaleBitmap(Bitmap bitmap, float px, int width, int height,
162
199
return Bitmap .createScaledBitmap (bitmap , w , h , true );
163
200
}
164
201
202
+ /**
203
+ * scales a given bitmap depending on the given size parameters.
204
+ *
205
+ * @param bitmap the bitmap to be scaled
206
+ * @param width the width
207
+ * @param height the height
208
+ * @return the scaled bitmap
209
+ */
210
+ public static Bitmap scaleBitmap (Bitmap bitmap , int width , int height ) {
211
+ return Bitmap .createScaledBitmap (bitmap , width , height , true );
212
+ }
213
+
165
214
/**
166
215
* Rotate bitmap according to EXIF orientation. Cf. http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/
167
216
*
@@ -230,6 +279,44 @@ public static Bitmap rotateImage(Bitmap bitmap, String storagePath) {
230
279
return resultBitmap ;
231
280
}
232
281
282
+ /**
283
+ * Detect if Image will be rotated according to EXIF orientation. Cf. http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/
284
+ *
285
+ * @param storagePath Path to source file of bitmap. Needed for EXIF information.
286
+ * @return true if image's orientation determines it will be rotated to where height and width change
287
+ */
288
+ public static boolean detectRotateImage (String storagePath ) {
289
+ try {
290
+ ExifInterface exifInterface = new ExifInterface (storagePath );
291
+ int orientation = exifInterface .getAttributeInt (ExifInterface .TAG_ORIENTATION , 1 );
292
+
293
+ if (orientation != ExifInterface .ORIENTATION_NORMAL ) {
294
+ switch (orientation ) {
295
+ // 5
296
+ case ExifInterface .ORIENTATION_TRANSPOSE : {
297
+ return true ;
298
+ }
299
+ // 6
300
+ case ExifInterface .ORIENTATION_ROTATE_90 : {
301
+ return true ;
302
+ }
303
+ // 7
304
+ case ExifInterface .ORIENTATION_TRANSVERSE : {
305
+ return true ;
306
+ }
307
+ // 8
308
+ case ExifInterface .ORIENTATION_ROTATE_270 : {
309
+ return true ;
310
+ }
311
+ }
312
+ }
313
+ }
314
+ catch (Exception exception ) {
315
+ Log_OC .e ("BitmapUtil" , "Could not read orientation at: " + storagePath );
316
+ }
317
+ return false ;
318
+ }
319
+
233
320
public static int [] getImageResolution (String srcPath ) {
234
321
Options options = new Options ();
235
322
options .inJustDecodeBounds = true ;
0 commit comments