From 76fe2b127673f2077193e4fa40be670e953df7cc Mon Sep 17 00:00:00 2001 From: sanduluca <56228534+sanduluca@users.noreply.github.com> Date: Sat, 20 Apr 2024 18:27:07 +0300 Subject: [PATCH 1/2] Decrease image quality when get OutOfMemoryError exception If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original. Reference: https://stackoverflow.com/questions/8442316/bitmap-is-returning-null-from-bitmapfactory-decodefilefilename --- .../websitebeaver/documentscanner/utils/ImageUtil.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/documentscanner/src/main/java/com/websitebeaver/documentscanner/utils/ImageUtil.kt b/documentscanner/src/main/java/com/websitebeaver/documentscanner/utils/ImageUtil.kt index b3cc247..78d1994 100644 --- a/documentscanner/src/main/java/com/websitebeaver/documentscanner/utils/ImageUtil.kt +++ b/documentscanner/src/main/java/com/websitebeaver/documentscanner/utils/ImageUtil.kt @@ -51,7 +51,14 @@ class ImageUtil { } // try reading image without OpenCV - var imageBitmap = BitmapFactory.decodeFile(filePath) + var imageBitmap + try { + imageBitmap = BitmapFactory.decodeFile(filePath) + } catch (OutOfMemoryError e) { + var options = new BitmapFactory.Options(); + options.inSampleSize = 2; + imageBitmap = BitmapFactory.decodeFile(filePath, options); + } val rotation = when (ExifInterface(filePath).getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL @@ -165,4 +172,4 @@ class ImageUtil { contentResolver.openInputStream(Uri.parse(fileUriString)) ) } -} \ No newline at end of file +} From 612044c0bf9b7d1838a399715be78a7e0be1031a Mon Sep 17 00:00:00 2001 From: Sandu Luca Date: Wed, 24 Apr 2024 18:59:05 +0300 Subject: [PATCH 2/2] fixed compilation errors - added type annotation - fixed Unresolved reference: new --- .../com/websitebeaver/documentscanner/utils/ImageUtil.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/documentscanner/src/main/java/com/websitebeaver/documentscanner/utils/ImageUtil.kt b/documentscanner/src/main/java/com/websitebeaver/documentscanner/utils/ImageUtil.kt index 78d1994..0478169 100644 --- a/documentscanner/src/main/java/com/websitebeaver/documentscanner/utils/ImageUtil.kt +++ b/documentscanner/src/main/java/com/websitebeaver/documentscanner/utils/ImageUtil.kt @@ -51,11 +51,11 @@ class ImageUtil { } // try reading image without OpenCV - var imageBitmap + var imageBitmap: Bitmap try { imageBitmap = BitmapFactory.decodeFile(filePath) - } catch (OutOfMemoryError e) { - var options = new BitmapFactory.Options(); + } catch (e: OutOfMemoryError) { + var options = BitmapFactory.Options(); options.inSampleSize = 2; imageBitmap = BitmapFactory.decodeFile(filePath, options); }