Fix QRCode scanner

This commit is contained in:
世界 2024-10-25 23:07:12 +08:00
parent 0de351375e
commit c6f71f5e47
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 29 additions and 8 deletions

View file

@ -75,10 +75,21 @@ class QRScanActivity : AbstractActivity<ActivityQrScanBinding>() {
}
private val onFailure: (Exception) -> Unit = {
lifecycleScope.launch {
errorDialogBuilder(it).show()
resetAnalyzer()
errorDialogBuilder("MLKit error: ${it.localizedMessage}").show()
}
}
private val vendorAnalyzer = Vendor.createQRCodeAnalyzer(onSuccess, onFailure)
private var useVendorAnalyzer = vendorAnalyzer != null
private fun resetAnalyzer() {
if (useVendorAnalyzer) {
useVendorAnalyzer = false
imageAnalysis.clearAnalyzer()
imageAnalyzer = ZxingQRCodeAnalyzer(onSuccess, onFailure)
imageAnalysis.setAnalyzer(analysisExecutor, imageAnalyzer)
}
}
private lateinit var cameraProvider: ProcessCameraProvider
private lateinit var cameraPreview: Preview
private lateinit var camera: Camera
@ -146,11 +157,19 @@ class QRScanActivity : AbstractActivity<ActivityQrScanBinding>() {
finish()
}
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
if (!useVendorAnalyzer) {
menu!!.findItem(R.id.action_use_vendor_analyzer).also {
it.isEnabled = false
it.isChecked = false
}
}
return true
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.qr_scan_menu, menu)
if (vendorAnalyzer == null) {
menu.findItem(R.id.action_use_vendor_analyzer).isEnabled = false
} else {
if (useVendorAnalyzer) {
menu.findItem(R.id.action_use_vendor_analyzer).isChecked = true
}
return true

View file

@ -29,7 +29,9 @@ class MLKitQRCodeAnalyzer(
override fun analyze(image: ImageProxy) {
if (image.image == null) return
if (failureOccurred && System.currentTimeMillis() - failureTimestamp < 1000L) {
val nowMills = System.currentTimeMillis()
if (failureOccurred && nowMills - failureTimestamp < 5000L) {
failureTimestamp = nowMills
Log.d("MLKitQRCodeAnalyzer", "throttled analysis since error occurred in previous pass")
image.close()
return

View file

@ -5,8 +5,8 @@ buildscript {
}
plugins {
id 'com.android.application' version '8.7.0' apply false
id 'com.android.library' version '8.7.0' apply false
id 'com.android.application' version '8.7.1' apply false
id 'com.android.library' version '8.7.1' apply false
id 'org.jetbrains.kotlin.android' version '1.9.23' apply false
id 'com.google.devtools.ksp' version '1.9.23-1.0.20' apply false
id 'com.github.triplet.play' version '3.8.4' apply false