Android QR Scanner using ML-Kit 🀳🏻

Android QR Scanner using      
ML-Kit 🀳🏻
Photo by Albert Hu / Unsplash

I have explored quite a bit for finding best ways of scanning QR/barcode in android. In my journey I have come across multiple libraries like ZXing, Quickie etc. and then when I tried the ML-kit barcode scanning, the results were Overwhelming.


ML-kit is very quick to scan and can recognise even when the Phone's camera is in a different orientation from QR code. upside-down, or sideways. (Other Libraries lacked this.)
Speed of the detection is amazingly fast when compared.
Works even when the lighting is very dim/bright. (extreme cases)
Also, the barcode detection is done on the device, and it requires no network connection.

Lets Jump to the Implementation:

There are two ways to implement this, Bundled and unbundled.


  • Feature is downloaded dynamically, Post the installation of the app.
  • App size increases only 300 Kb
  • First time loading (before you scan a barcode) is a bit long.


  • App size increases by 3.2 Mb
  • Scanning is available immediately, No loading.

For this article, Let's go ahead with the bundled one.

πŸ”— Dependencies needed:

  1. ML-Kit Barcode scanning
    for processing the images, finding barcode in them (Live).
implementation '<Latest-version>'

2. CameraX
 for displaying a customised camera layout.

implementation '<latest-version>'
implementation '<latest-version>'
implementation '<latest-version>'
implementation '<latest-version>'


Let's Jump to the code now.
We will need a new Activity/ fragment to host the Camera Preview, Along with the QR scanner overlay.

PreviewView for displaying the camera

        android:layout_height="match_parent" />

For QR Scanner overlay, you can find many on the internet.
Here are a few.

QR-BarcodeScanner/ViewFinderOverlay.kt at main Β· KiranShny/QR-BarcodeScanner
An app for bar code scanning. Contribute to KiranShny/QR-BarcodeScanner development by creating an account on GitHub.
Basic QR Scanner overlay for Android. Originally made by Ravi Tamada.
Basic QR Scanner overlay for Android. Originally made by Ravi Tamada. - ScannerOverlay.kt

Also, add a Button for Turning the flash on/off.

πŸ“Έ Camera Setup:

Selecting the back camera:

cameraSelector = CameraSelector.Builder()

Bind the Camera Preview to the Lifecycle of the activity.

previewUseCase = Preview.Builder()

val camera = cameraProvider.bindToLifecycle(

πŸ”Ž Image Analysis:

You can filter what type of QR/Barcode are you looking for in the image

val options = BarcodeScannerOptions.Builder()

OR, you can ignore this, and you get results for all kinds of QR/barcode.

ML-Kit's BarcodeScanning class has the method for processing the Image that we get from the Camera.

 val barcodeScanner: BarcodeScanner = BarcodeScanning.getClient()
 analysisUseCase = ImageAnalysis.Builder()
analysisUseCase.setAnalyzer(imageExecutor) { imageProxy ->
    val inputImage =

        .addOnSuccessListener { barcodes ->
                barcodes.forEach { barcode ->
                    barcode.rawValue //This contains the plainText value
            .addOnFailureListener { error ->
            .addOnCompleteListener {

The obtained Barcode class will contain all necessary details like - Type of the barcode, contents, etc.

Thank you.

Further reading:

Barcode scanning | ML Kit | Google Developers

Check out my project on github:

I have built an app for barcode scanning,
It also has a QuickTile support for scanning from the notification bar quick settings.
Let's discuss about that in a different article.

GitHub - KiranShny/QR-BarcodeScanner: An app for bar code scanning
An app for bar code scanning. Contribute to KiranShny/QR-BarcodeScanner development by creating an account on GitHub.