How To Use Google’s ML Kit To Scan Barcodes

Hey Everyone!

Recently one of my clients asked me to implement barcode scanning features in an Android application, which I successfully implemented using Google’s ML Kit. In this article we will learn about Google ML Kit and how can we integrate in Android application.

What is ML Kit?

Why Google’s ML Kit?

There was also an other reason for this quick shift to new ML Kit. Because, It is not possible to use ML Kit for Firebase as a standalone SDK. Any application that want to use any service/product or library, that is part of Firebase, must create a Firebase project and also integrate Firebase core features or SDK in it. And it is a prerequisites for all application with or without Firebase online features implementation. By the way, integrating Firebase in an Android application is totally different topic which I have discussed here in detail. Because, you might still want to use another service from Firebase and that article will definitely help you in doing so.

ML Kit Features

  • Vision APIs
  • Natural Language APIs

Vision APIs

Natural Language APIs

Barcode Scanning Features

ML Kit’s barcode scanning API also detect all of the formats automatically and it can also tell you about the scanned format, that you can use for any validation in your application. But, my client was only looking for support of Code 39 and QR code for now. That’s why we have to limit ML Kit barcode scanning API to only support these two formats in our application.

Some other interesting features of this API are extraction of structured data, different type of orientations support and performance boost, because, it run on your device itself without any network connection.

How to integrate ML Kit in Android application

1. Open you project-level build.gradle file and make sure it has Maven repository from Google and obviously Google’s own repository listed in buildscript and allprojects sections like this.

buildscript {

repositories {
mavenLocal()
google()
...

}
}
allprojects {
repositories {
mavenLocal()
google()
...
}
}

2. Add dependency for ML Kit Barcode scanning model in your application. As we are only using ML Kit for Barcode scanning that’s why we only need to add this model/dependency. You can add this dependency in two different ways. 1. Bundle it with your app (This is what we will be doing), 2. Configure your app to dynamically download it from Google Play services on first run.

To bundle it with your app. You will have to add this dependency in dependencies section of your app-level build.gradle file.

dependencies {
...
implementation 'com.google.mlkit:barcode-scanning:16.0.0'
...
}

Now go ahead and sync your project to let Gradle download or update all dependencies for you in the background. You can verify your integration by adding support for “com.google.mlkit.vision.barcode.*” package in one of your application’s activity or class. If you don’t see any error it means you have integrated it successfully. In case of any error, please check both build.gradle files and Android Studio’s Event Log for more details.

Configuring Barcode Scanner

For example, in our app we wanted to scan only Code 39 and QR code type formats. That’s why we restricted our BarcodeScanner to only these two formats. You can also do the same by creating BarcodeScannerOptions object and passing it to your BarcodeScanner object. Here is a code snippet to do this.

BarcodeScannerOptions options = new BarcodeScannerOptions.Builder()
.setBarcodeFormats(
Barcode.FORMAT_QR_CODE,
Barcode.FORMAT_CODE_39
).build();
// This is how you can get an instance of BarcodeScanner
BarcodeScanner barcodeScanner = BarcodeScanning.getClient(options);

These are the bare minimum configurations and requirements to integrate Google ML Kit based Barcode scanning in your Android application. You should be able to integrate Google ML Kit in your Android application by following these steps.

Send Data or Information to Scanner

  1. Using an image from your device Camera feed
  2. Using an image from device media gallery
  3. Using a ByteBuffer or ByteArray
  4. Using a Bitmap

In our Barcode scanning android application, we used option #1 by using live camera feed as an image source or InputImage to our scanner instance. And to implement camera features in our application we used latest CameraX APIs. I will share another article to explain CameraX integration in Android application later.

Process Information

BarcodeScanner‘s process() function take an InputImage parameter and implement various different listener or callback functions, that also includes OnSuccessListener and OnFailureListner. Here is how you can pass InputImage to process() function and implement required listeners.

barcodeScanner.process(inputImage)
.addOnSuccessListener(new OnSuccessListener<List<Barcode>>() {
@Override
public void onSuccess(List<Barcode> barcodes) {
// Process scanned information here
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Process failed
}
});

Scanned barcodes will be returned as a List<Barcode> object to OnSuccessListener. Don’t forget to check list size and barcode format in your logic.

// In onSuccess function of OnSuccessListener
...
if (barcodes != null && barcodes.size() > 0) {
for (Barcode barcode: barcodes) {
int scannedFormat = barcode.getFormat();
switch (scannedFormat) {
case Barcode.FORMAT_QR_CODE:
// Handle logic for QR code
break;
case Barcode.FORMAT_CODE_39:
// Handle logic for Core 39
break;
default:
// Handle logic in case of none of the
// required formats
break;
}
}
}
...

You can just get raw value of scanned barcode and use accordingly. Or you can also get value of scanned barcodes as a structured value/object and process it accordingly. Because ML Kit’s BarcodeScanner can identify different type of objects in scanned barcode and return values accordingly. Value types can include WIFI address, URL, contact, etc. You can complete list of types here.

Conclusion

By the way, please don’t forget to check complete code sample of scanning barcode with ML-Kit here in one of my GitHub repositories.

Also don’t forget to share your valuable feedback and suggestions for this article. And if you have enjoyed reading this article and it was of any help, please share it with others to help them as well.

You can also see list of all of my guides and tutorials here. I hope to see you soon in another guide or tutorial some other time.

Allah Hafiz.

This guide was first appeared on the Zeeshan Elahi’s website at zeeshanelahi.com.

I am a son, husband, father, brother, friend, software engineering professional and fitness enthusiast. I love reading, writing, vlogging and sharing knowledge.