How To Use Google’s ML Kit To Scan Barcodes

Zeeshan Elahi
7 min readJun 29, 2020

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?

ML Kit is a standalone library from Google with on-device machine learning capabilities. It is easy-to-use and can be integrated in any Android and iOS application in few simple steps. And most importantly, it brings Google’s machine learning knowledge and expertise to our mobile application.

Why Google’s ML Kit?

Although, I have worked with Zxing barcode library in past for some other clients. But, it was long time ago. Therefore, I decided to give a try to a new library or SDK with some extra features and capabilities. Also Google’s ML Kit was part of Firebase at the time when I kick-started my implementation. And it was initially known as ML Kit for Firebase. You can read more about it here. But, later when Google introduced ML Kit as a standalone library or SDK on June 3rd, 2020. We both were eager, me and my client, to give it a try and replace ML Kit for Firebase with this library. As per my understanding, I might be in one of those few people who started integrating Google ML Kit in android applications after 2–3 days of it’s release as a separate SDK.

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

Google’s ML Kit provide implementation for two different set of APIs. That includes:

  • Vision APIs
  • Natural Language APIs

Vision APIs

Vision APIs handle features related to barcode scanning, face and object detection, object tracking, image labeling etc. You can use any or all of these APIs with LIVE camera feed or simple bitmap or images.

Natural Language APIs

Natural Language APIs handle features related to language identification and translation. It can help you to identify and translate text from almost 50+ different languages. It also help you to generate smart reply suggestions in your chats and email applications.

Barcode Scanning Features

ML Kit’s barcode scanning API can read and scan almost dozen different type of barcodes. That includes both linear and 2D formats. For example, Codabar, Code 39, Code 93, EAN-8, EAN-13, QR code, PDF417, etc. You can see complete list of supported Barcode formats here.

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

There are some important steps or checks that you need to do before you can actually start using ML Kit in you 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

Now you just need to initialize the BarcodeScanner in your app and pass it your target object, it can be camera feed, a bitmap, a media file, etc. and must be passed as InputImage object. And it will return you results and scanned barcode value if it found one in the InputImage object. But, it’s good idea to restrict your BarcodeScanner to look for only required barcode format.

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

ML Kit BarcodeScanner need an InputImage object to process and find barcode. InputImage is part of com.google.mlkit.vision.common package and you can initialize it in four different ways.

  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

When you have InputImage object initialized by using one of the four above methods, you can pass it to BarcodeScanner to process and return results or scanned 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

Google’s ML Kit is great in many different ways. Barcode scanning is just one small part of it. I will definitely try and discuss other ML Kit features in my future articles. You should also give a try to all or some of the ML Kit features yourself and do let me know about your experience in comments below.

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.

--

--

Zeeshan Elahi

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