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/helper/OSBARCMLKitHelper.kt b/src/main/kotlin/com/outsystems/plugins/barcode/controller/helper/OSBARCMLKitHelper.kt index e86212a..cfdb132 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,7 +13,10 @@ 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: OSBARCMLKitHelperInterface { +class OSBARCMLKitHelper(private val delayMillis: Long? = 500L): OSBARCMLKitHelperInterface { + + private var lastAnalyzedTimestamp = 0L + companion object { private const val LOG_TAG = "OSBARCMLKitHelper" } @@ -31,22 +34,32 @@ class OSBARCMLKitHelper: OSBARCMLKitHelperInterface { onSuccess: (MutableList) -> Unit, onError: () -> Unit ) { - 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() - } + 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 + } } } \ No newline at end of file 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..13a61a1 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() + OSBARCMLKitHelper(parameters.scanInterval) ), OSBARCImageHelper(), { result ->