Skip to content

Commit

Permalink
Implemented delay on analyzer rather than helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
umer-ppup committed Jun 10, 2024
1 parent 45ae7cd commit 627d65c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -34,32 +31,22 @@ class OSBARCMLKitHelper(private val delayMillis: Long? = 500L): OSBARCMLKitHelpe
onSuccess: (MutableList<Barcode>) -> 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()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,16 @@ class OSBARCScannerActivity : ComponentActivity() {
OSBARCScanLibraryFactory.createScanLibraryWrapper(
parameters.androidScanningLibrary ?: "",
OSBARCZXingHelper(),
OSBARCMLKitHelper(parameters.scanInterval)
OSBARCMLKitHelper()
),
OSBARCImageHelper(),
{ result ->
processReadSuccess(result)
},
{
processReadError(it)
}
},
parameters.scanInterval
)

setContent {
Expand Down

0 comments on commit 627d65c

Please sign in to comment.