0

我尝试使用以下方法将 QR 码扫描仪添加到我的 Android 应用程序:

fun scanQRCode(bitmap: Bitmap): String? {
    val options = FirebaseVisionBarcodeDetectorOptions.Builder()
            .setBarcodeFormats(FirebaseVisionBarcode.FORMAT_QR_CODE)
            .build()
    val detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options);
    val image = FirebaseVisionImage.fromBitmap(bitmap)
    var id: String? = ""
    detector.detectInImage(image).addOnSuccessListener {
        if (it.isEmpty()) {
            id = null
            return@addOnSuccessListener
        }
        for (firebaseBarcode in it) {
            val a = it[0].rawValue ?: ""
            id = a

        }
    }.addOnFailureListener {
        id = null
    }
    return id
}

运行应用程序时,既onFailure不会onSuccess触发回调,也不会触发回调。我的 id 总是返回 null 并且我在 logcat 中收到以下警告:

W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.dynamite.barcode not found.
I/DynamiteModule: Considering local module com.google.android.gms.vision.dynamite.barcode:0 and remote module com.google.android.gms.vision.dynamite.barcode:0
D/BarcodeNativeHandle: Cannot load feature, fall back to load dynamite module.
I/DynamiteModule: Considering local module com.google.android.gms.vision.barcode:0 and remote module com.google.android.gms.vision.barcode:1
I/DynamiteModule: Selected remote version of com.google.android.gms.vision.barcode, version >= 1

我已经在我的测试手机(HTC Desire 19+)上检查了我的互联网连接,并删除了 Google Play 服务的本地缓存。

我对 qr 扫描的 gradle 依赖项如下:

implementation "com.google.firebase:firebase-ml-model-interpreter:22.0.3"
implementation "com.google.firebase:firebase-ml-vision:24.0.3"
implementation 'com.google.firebase:firebase-core:17.4.1'
implementation 'com.google.android.gms:play-services-vision:20.0.0'

以前有人遇到过这个吗?这是我的代码库中的问题还是火力库问题?

4

1 回答 1

0

我发现了错误,它实际上在我的代码库中。的返回值scanQRCode()是扫描结果值,但是扫描过程是异步的,所以它总是返回一个空字符串。我通过添加回调来传递值来修复错误:

fun scanQRCode(bitmap: Bitmap, callback: QRInterface) {
    val options = FirebaseVisionBarcodeDetectorOptions.Builder()
            .setBarcodeFormats(FirebaseVisionBarcode.FORMAT_QR_CODE)
            .build()
    val detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options);
    val image = FirebaseVisionImage.fromBitmap(bitmap)
    detector.detectInImage(image).addOnSuccessListener {
        if (it.isEmpty()) {
            callback.onQRScanFinished(null)
            return@addOnSuccessListener
        }
        for (firebaseBarcode in it) {
            val a = it[0].rawValue ?: ""
            callback.onQRScanFinished(a)
            Log.d("QRScanner", "successful")

        }
    }.addOnFailureListener {
        it.printStackTrace()
        callback.onQRScanFinished(null)

        Log.d("QRScanner", "failed")
    }
}
于 2020-05-12T21:30:01.643 回答