diff --git a/src/main/kotlin/com/outsystems/plugins/barcode/controller/OSBARCBarcodeAnalyzer.kt b/src/main/kotlin/com/outsystems/plugins/barcode/controller/OSBARCBarcodeAnalyzer.kt index f238a1b..f2c98ec 100644 --- a/src/main/kotlin/com/outsystems/plugins/barcode/controller/OSBARCBarcodeAnalyzer.kt +++ b/src/main/kotlin/com/outsystems/plugins/barcode/controller/OSBARCBarcodeAnalyzer.kt @@ -18,10 +18,12 @@ class OSBARCBarcodeAnalyzer( private val scanLibrary: OSBARCScanLibraryInterface, private val imageHelper: OSBARCImageHelperInterface, private val onBarcodeScanned: (String) -> Unit, - private val onScanningError: (OSBARCError) -> Unit + private val onScanningError: (OSBARCError) -> Unit, + private val delayMillis: Long? = 500L ): ImageAnalysis.Analyzer { var isPortrait = true + private var lastAnalyzedTimestamp = 0L companion object { private const val LOG_TAG = "OSBARCBarcodeAnalyzer" @@ -34,20 +36,30 @@ class OSBARCBarcodeAnalyzer( * @param image - ImageProxy object that represents the image to be analyzed. */ override fun analyze(image: ImageProxy) { - try { - scanLibrary.scanBarcode( - image, - cropBitmap(image.toBitmap()), - { - onBarcodeScanned(it) - }, - { - onScanningError(it) - } - ) - } catch (e: Exception) { - e.message?.let { Log.e(LOG_TAG, it) } - onScanningError(OSBARCError.SCANNING_GENERAL_ERROR) + val currentTimestamp = System.currentTimeMillis() + + // Use the delayMillis value, defaulting to 500 milliseconds if it is null + val effectiveDelayMillis = delayMillis ?: 500L + + // Only analyze the image if the desired delay has passed + if (currentTimestamp - lastAnalyzedTimestamp >= effectiveDelayMillis) { + try { + scanLibrary.scanBarcode( + image, + cropBitmap(image.toBitmap()), + { + onBarcodeScanned(it) + }, + { + onScanningError(it) + } + ) + } catch (e: Exception) { + e.message?.let { Log.e(LOG_TAG, it) } + onScanningError(OSBARCError.SCANNING_GENERAL_ERROR) + } + // Update the timestamp of the last analyzed frame + lastAnalyzedTimestamp = currentTimestamp } image.close() } diff --git a/src/main/kotlin/com/outsystems/plugins/barcode/controller/helper/OSBARCMLKitHelper.kt b/src/main/kotlin/com/outsystems/plugins/barcode/controller/helper/OSBARCMLKitHelper.kt index cfdb132..e86212a 100644 --- a/src/main/kotlin/com/outsystems/plugins/barcode/controller/helper/OSBARCMLKitHelper.kt +++ b/src/main/kotlin/com/outsystems/plugins/barcode/controller/helper/OSBARCMLKitHelper.kt @@ -13,10 +13,7 @@ import com.google.mlkit.vision.common.InputImage * to scan an image using the ML Kit library. * It encapsulates all the code related with the ML Kit library. */ -class OSBARCMLKitHelper(private val delayMillis: Long? = 500L): OSBARCMLKitHelperInterface { - - private var lastAnalyzedTimestamp = 0L - +class OSBARCMLKitHelper: OSBARCMLKitHelperInterface { companion object { private const val LOG_TAG = "OSBARCMLKitHelper" } @@ -34,32 +31,22 @@ class OSBARCMLKitHelper(private val delayMillis: Long? = 500L): OSBARCMLKitHelpe onSuccess: (MutableList) -> Unit, onError: () -> Unit ) { - val currentTimestamp = System.currentTimeMillis() - - // Use the delayMillis value, defaulting to 500 milliseconds if it is null - val effectiveDelayMillis = delayMillis ?: 500L - - // Only analyze the image if the desired delay has passed - if (currentTimestamp - lastAnalyzedTimestamp >= effectiveDelayMillis) { - val options = BarcodeScannerOptions.Builder() - .enableAllPotentialBarcodes() - .build() - val scanner = BarcodeScanning.getClient(options) - val image = InputImage.fromBitmap( - imageBitmap, - imageProxy.imageInfo.rotationDegrees - ) - scanner.process(image) - .addOnSuccessListener { barcodes -> - onSuccess(barcodes) - } - .addOnFailureListener { e -> - e.message?.let { Log.e(LOG_TAG, it) } - onError() - } - // Update the timestamp of the last analyzed frame - lastAnalyzedTimestamp = currentTimestamp - } + val options = BarcodeScannerOptions.Builder() + .enableAllPotentialBarcodes() + .build() + val scanner = BarcodeScanning.getClient(options) + val image = InputImage.fromBitmap( + imageBitmap, + imageProxy.imageInfo.rotationDegrees + ) + scanner.process(image) + .addOnSuccessListener { barcodes -> + onSuccess(barcodes) + } + .addOnFailureListener { e -> + e.message?.let { Log.e(LOG_TAG, it) } + onError() + } } } \ No newline at end of file diff --git a/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt b/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt index 13a61a1..30855e9 100644 --- a/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt +++ b/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt @@ -178,7 +178,7 @@ class OSBARCScannerActivity : ComponentActivity() { OSBARCScanLibraryFactory.createScanLibraryWrapper( parameters.androidScanningLibrary ?: "", OSBARCZXingHelper(), - OSBARCMLKitHelper(parameters.scanInterval) + OSBARCMLKitHelper() ), OSBARCImageHelper(), { result -> @@ -186,7 +186,8 @@ class OSBARCScannerActivity : ComponentActivity() { }, { processReadError(it) - } + }, + parameters.scanInterval ) setContent {