diff --git a/build.gradle b/build.gradle index 1c8922e..7d145f4 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:8.1.1' + classpath 'com.android.tools.build:gradle:8.1.4' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jacoco:org.jacoco.core:$jacocoVersion" } 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/model/OSBARCScanParameters.kt b/src/main/kotlin/com/outsystems/plugins/barcode/model/OSBARCScanParameters.kt index 502efb2..8be1be1 100644 --- a/src/main/kotlin/com/outsystems/plugins/barcode/model/OSBARCScanParameters.kt +++ b/src/main/kotlin/com/outsystems/plugins/barcode/model/OSBARCScanParameters.kt @@ -13,5 +13,6 @@ data class OSBARCScanParameters( @SerializedName("scanButton") val scanButton: Boolean, @SerializedName("scanText") val scanText: String, @SerializedName("hint") val hint: Int?, - @SerializedName("androidScanningLibrary") val androidScanningLibrary: String? + @SerializedName("androidScanningLibrary") val androidScanningLibrary: String?, + @SerializedName("scanInterval") val scanInterval: Long? ) : Serializable \ 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 d420a92..30855e9 100644 --- a/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt +++ b/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt @@ -186,7 +186,8 @@ class OSBARCScannerActivity : ComponentActivity() { }, { processReadError(it) - } + }, + parameters.scanInterval ) setContent {