Skip to content

Commit

Permalink
Added a scanning interval on processing image for barcode.
Browse files Browse the repository at this point in the history
  • Loading branch information
umer-ppup committed Jun 10, 2024
1 parent 7379b57 commit 45ae7cd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -31,22 +34,32 @@ class OSBARCMLKitHelper: OSBARCMLKitHelperInterface {
onSuccess: (MutableList<Barcode>) -> 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
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class OSBARCScannerActivity : ComponentActivity() {
OSBARCScanLibraryFactory.createScanLibraryWrapper(
parameters.androidScanningLibrary ?: "",
OSBARCZXingHelper(),
OSBARCMLKitHelper()
OSBARCMLKitHelper(parameters.scanInterval)
),
OSBARCImageHelper(),
{ result ->
Expand Down

0 comments on commit 45ae7cd

Please sign in to comment.