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

@ -62,8 +62,8 @@ class QRScanActivity : AbstractActivity<ActivityQrScanBinding>() {
} else {
setResult(RESULT_CANCELED)
finish()
}
}
}
private lateinit var imageAnalysis: ImageAnalysis
private lateinit var imageAnalyzer: ImageAnalysis.Analyzer
@ -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