From c6f71f5e47e5f2849824c0d1f01f2cadd408a938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Fri, 25 Oct 2024 23:07:12 +0800 Subject: [PATCH] Fix QRCode scanner --- .../sfa/ui/profile/QRScanActivity.kt | 29 +++++++++++++++---- .../sfa/vendor/MLKitQRCodeAnalyzer.kt | 4 ++- build.gradle | 4 +-- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/nekohasekai/sfa/ui/profile/QRScanActivity.kt b/app/src/main/java/io/nekohasekai/sfa/ui/profile/QRScanActivity.kt index 7d047e0..ce3dedd 100644 --- a/app/src/main/java/io/nekohasekai/sfa/ui/profile/QRScanActivity.kt +++ b/app/src/main/java/io/nekohasekai/sfa/ui/profile/QRScanActivity.kt @@ -62,8 +62,8 @@ class QRScanActivity : AbstractActivity() { } else { setResult(RESULT_CANCELED) finish() + } } - } private lateinit var imageAnalysis: ImageAnalysis private lateinit var imageAnalyzer: ImageAnalysis.Analyzer @@ -75,10 +75,21 @@ class QRScanActivity : AbstractActivity() { } 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() { 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 diff --git a/app/src/play/java/io/nekohasekai/sfa/vendor/MLKitQRCodeAnalyzer.kt b/app/src/play/java/io/nekohasekai/sfa/vendor/MLKitQRCodeAnalyzer.kt index 9e27c92..4ac8132 100644 --- a/app/src/play/java/io/nekohasekai/sfa/vendor/MLKitQRCodeAnalyzer.kt +++ b/app/src/play/java/io/nekohasekai/sfa/vendor/MLKitQRCodeAnalyzer.kt @@ -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 diff --git a/build.gradle b/build.gradle index 5765378..7a43853 100644 --- a/build.gradle +++ b/build.gradle @@ -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