I tried to add a QR code scanner to my Android app with the following method:
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
}
When running the app neither the onFailure
nor the onSuccess
callbacks are triggered. My id returns always null and I get the following warnings in 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
I already checked for my internet connection on my testing phone (HTC Desire 19+) and deleted the local cache of Google Play Services.
My gradle dependencies for the qr scanning are the following:
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'
Anyone ran into this before? Is it rather an issue in my codebase or a firebase issue?
I found the error, it was actually in my codebase. The return value for scanQRCode()
was the scanned result value, but the scanning process is asynchronus, so it always returned an empty string.
I fixed the error by adding a callback to pass the value:
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")
}
}