Skip to content

Commit 98c830b

Browse files
committed
fix: camera cool down
1 parent 43946a0 commit 98c830b

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

app/src/main/java/to/bitkit/ui/screens/scanner/QrCodeAnalyzer.kt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@ import to.bitkit.utils.Logger
1616
class QrCodeAnalyzer(
1717
private val onScanResult: (Result<String>) -> Unit,
1818
) : ImageAnalysis.Analyzer {
19-
private var isScanning = true
19+
private var lastScannedCode: String? = null
20+
private var lastScanTime: Long = 0
21+
private val scanCooldownMs = 2000L // 2 seconds cooldown between scans
2022

2123
private val scannerOptions = BarcodeScannerOptions.Builder()
2224
.setBarcodeFormats(Barcode.FORMAT_QR_CODE)
2325
.build()
2426
private val scanner: BarcodeScanner = BarcodeScanning.getClient(scannerOptions)
2527

26-
override fun analyze(image: ImageProxy) {
27-
if (!isScanning) {
28-
image.close()
29-
return
30-
}
28+
fun reset() {
29+
lastScannedCode = null
30+
lastScanTime = 0
31+
}
3132

33+
override fun analyze(image: ImageProxy) {
3234
if (image.image != null) {
3335
val inputImage = InputImage.fromMediaImage(image.image!!, image.imageInfo.rotationDegrees)
3436
scanner.process(inputImage)
@@ -37,8 +39,15 @@ class QrCodeAnalyzer(
3739
it.result.let { barcodes ->
3840
barcodes.forEach { barcode ->
3941
barcode.rawValue?.let { qrCode ->
40-
isScanning = false
41-
onScanResult(Result.success(qrCode))
42+
val currentTime = System.currentTimeMillis()
43+
val isDifferentCode = qrCode != lastScannedCode
44+
val isCooldownExpired = currentTime - lastScanTime > scanCooldownMs
45+
46+
if (isDifferentCode || isCooldownExpired) {
47+
lastScannedCode = qrCode
48+
lastScanTime = currentTime
49+
onScanResult(Result.success(qrCode))
50+
}
4251
image.close()
4352
return@addOnCompleteListener
4453
}

0 commit comments

Comments
 (0)