@@ -4,7 +4,11 @@ import android.annotation.SuppressLint
4
4
import android.app.Activity
5
5
import android.content.Context
6
6
import android.graphics.Bitmap
7
+ import android.graphics.Canvas
8
+ import android.graphics.ColorMatrix
9
+ import android.graphics.ColorMatrixColorFilter
7
10
import android.graphics.Matrix
11
+ import android.graphics.Paint
8
12
import android.graphics.Rect
9
13
import android.hardware.display.DisplayManager
10
14
import android.media.Image
@@ -141,14 +145,13 @@ class MobileScanner(
141
145
mobileScannerCallback(
142
146
barcodeMap,
143
147
null ,
144
- if (portrait) mediaImage .width else mediaImage .height,
145
- if (portrait) mediaImage .height else mediaImage .width)
148
+ if (portrait) inputImage .width else inputImage .height,
149
+ if (portrait) inputImage .height else inputImage .width)
146
150
return @addOnSuccessListener
147
151
}
148
152
149
153
val bitmap = Bitmap .createBitmap(mediaImage.width, mediaImage.height, Bitmap .Config .ARGB_8888 )
150
- val imageFormat = YuvToRgbConverter (activity.applicationContext)
151
-
154
+ val imageFormat = YuvToRgbConverter (activity.applicationContext)
152
155
imageFormat.yuvToRgb(mediaImage, bitmap)
153
156
154
157
val bmResult = rotateBitmap(bitmap, camera?.cameraInfo?.sensorRotationDegrees?.toFloat() ? : 90f )
@@ -159,6 +162,7 @@ class MobileScanner(
159
162
val bmWidth = bmResult.width
160
163
val bmHeight = bmResult.height
161
164
bmResult.recycle()
165
+ imageFormat.release()
162
166
163
167
mobileScannerCallback(
164
168
barcodeMap,
@@ -233,8 +237,7 @@ class MobileScanner(
233
237
mobileScannerStartedCallback : MobileScannerStartedCallback ,
234
238
mobileScannerErrorCallback : (exception: Exception ) -> Unit ,
235
239
detectionTimeout : Long ,
236
- cameraResolution : Size ? ,
237
- newCameraResolutionSelector : Boolean ,
240
+ cameraResolutionWanted : Size ? ,
238
241
shouldConsiderInvertedImages : Boolean ,
239
242
) {
240
243
this .detectionSpeed = detectionSpeed
@@ -492,40 +495,45 @@ class MobileScanner(
492
495
/* *
493
496
* Inverts the image colours respecting the alpha channel
494
497
*/
495
- @SuppressLint( " UnsafeOptInUsageError " )
498
+ @ExperimentalGetImage
496
499
fun invertInputImage (imageProxy : ImageProxy ): InputImage {
497
500
val image = imageProxy.image ? : throw IllegalArgumentException (" Image is null" )
498
501
499
- // Convert YUV_420_888 image to NV21 format
500
- // based on our util helper
502
+ // Convert YUV_420_888 image to RGB Bitmap
501
503
val bitmap = Bitmap .createBitmap(image.width, image.height, Bitmap .Config .ARGB_8888 )
502
- YuvToRgbConverter (activity).yuvToRgb(image, bitmap)
503
-
504
- // Invert RGB values
505
- invertBitmapColors(bitmap)
506
-
507
- return InputImage .fromBitmap(bitmap, imageProxy.imageInfo.rotationDegrees)
504
+ try {
505
+ val imageFormat = YuvToRgbConverter (activity.applicationContext);
506
+ imageFormat.yuvToRgb(image, bitmap)
507
+
508
+ // Create an inverted bitmap
509
+ val invertedBitmap = invertBitmapColors(bitmap)
510
+ imageFormat.release()
511
+
512
+ return InputImage .fromBitmap(invertedBitmap, imageProxy.imageInfo.rotationDegrees)
513
+ } finally {
514
+ // Release resources
515
+ bitmap.recycle() // Free up bitmap memory
516
+ imageProxy.close() // Close ImageProxy
517
+ }
508
518
}
509
519
510
- // Helper function to invert the colors of the bitmap
511
- private fun invertBitmapColors (bitmap : Bitmap ) {
512
- val width = bitmap.width
513
- val height = bitmap.height
514
- for (x in 0 until width) {
515
- for (y in 0 until height) {
516
- val pixel = bitmap.getPixel(x, y)
517
- val invertedColor = invertColor(pixel)
518
- bitmap.setPixel(x, y, invertedColor)
519
- }
520
+ // Efficiently invert bitmap colors using ColorMatrix
521
+ private fun invertBitmapColors (bitmap : Bitmap ): Bitmap {
522
+ val colorMatrix = ColorMatrix ().apply {
523
+ set(floatArrayOf(
524
+ - 1f , 0f , 0f , 0f , 255f , // Red
525
+ 0f , - 1f , 0f , 0f , 255f , // Green
526
+ 0f , 0f , - 1f , 0f , 255f , // Blue
527
+ 0f , 0f , 0f , 1f , 0f // Alpha
528
+ ))
520
529
}
521
- }
530
+ val paint = Paint ().apply { colorFilter = ColorMatrixColorFilter (colorMatrix) }
531
+
532
+ val invertedBitmap = Bitmap .createBitmap(bitmap.width, bitmap.height, bitmap.config)
533
+ val canvas = Canvas (invertedBitmap)
534
+ canvas.drawBitmap(bitmap, 0f , 0f , paint)
522
535
523
- private fun invertColor (pixel : Int ): Int {
524
- val alpha = pixel and 0xFF000000 .toInt()
525
- val red = 255 - (pixel shr 16 and 0xFF )
526
- val green = 255 - (pixel shr 8 and 0xFF )
527
- val blue = 255 - (pixel and 0xFF )
528
- return alpha or (red shl 16 ) or (green shl 8 ) or blue
536
+ return invertedBitmap
529
537
}
530
538
531
539
/* *
0 commit comments