4

I'm trying to implement a QR code scanner in my app using Mlkit and following these two links :

https://medium.com/codex/scan-barcodes-in-android-using-the-ml-kit-30b2a03ccd50

https://developers.google.com/ml-kit/vision/barcode-scanning/android

Mlkit and CameraX versions :

camerax_version = "1.1.0-beta03" / barcodeScanning = "17.0.2"

It works fine on any Samsung device, but on Xiaomi mi 9T pro with Android 10 it takes between 2 and 10 secondes to scan a simple QR code. On Pixel 2 with Android 11 it sometimes never work or takes up to 20 secondes to work.

When I use the following lib it works fine on every device :

Android zxing Embedded BarcodeView not resuming

Why does Mlkit take so long (or don't work) to scan some simple QR code on some device ?

Thanks for the response

2 Answers2

1

if you are using MLkit barcode scanning and getting process image result is slow, then use below code.it responds fast.

if (image.image != null && image.format == ImageFormat.YUV_420_888) {
                


                scanner.process(InputImage.fromBitmap(image.toBitmap(), image.imageInfo.rotationDegrees))
                    .addOnSuccessListener { barcodes ->
                        if (barcodes.isNotEmpty()) {

                            if (firstCall){


                                for (barcode in barcodes) {
                                    

                                    // Handle received barcodes...


                                    Log.d("ValueXXXX ",barcode.format.toString())

                                    if (barcode.format==Barcode.FORMAT_QR_CODE){
                                        var data = barcode.displayValue.toString().replace("\\", "")
                                        //var json=org.json.JSONObject(data)

                                        Log.d("TAG","DATA1111 "+data)

                                        if(Utils.urlPattern.matcher(data).matches()) {
                                            firstCall=false


                                            showPopupForInvalidQRCode(R.string.DISPLAY_INVALID_QR_CODE,context)



                                            break
                                        }
                                        if (!data.isNullOrEmpty() && Utils.isJSONValid(data)){
                                            firstCall=false
                                            try {
                                                val json = JSONObject(data)
                                                Log.d("TAG","json "+json)
                                                val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
                                                    if (Build.VERSION.SDK_INT >= 26) {
                                                        vibrator.vibrate(VibrationEffect.createOneShot(300, VibrationEffect.DEFAULT_AMPLITUDE))
                                                    } else {
                                                        vibrator.vibrate(300)
                                                    }

                                                 //parse your json here   

                                                break
                                            }catch (e:Exception){
                                                e.printStackTrace()
                                                showPopupForInvalidQRCode(R.string.DISPLAY_INVALID_QR_CODE,context)
                                            }
                                        }

                                        break
                                    }



                                }
                            }

                        } else {
                            // Remove bounding rect
                            barcodeBoxView.setRect(RectF())
                        }
                    }
                    .addOnFailureListener {
                        firstCall=true
                        Log.d("EXCEPTION",it.message.toString())
                        //image.close()

                    }
                    

                    }
            }
Anshul Nema
  • 281
  • 3
  • 3
0

If you are using the default camera configuration it is expected to be slow for sure because the images are too big, you need to something like

.setTargetResolution(Size(1200, 1600))

By the way, you need to check the list of supported resolutions because not any resolution will work for any phone, please check

https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics#SENSOR_INFO_PIXEL_ARRAY_SIZE

Mohammad Elsayed
  • 1,885
  • 1
  • 20
  • 46