Hitanshu Dhawan
Oct 22, 2018 · 4 min read

Barcode Scanning is used to read the data encoded in the barcodes.

Barcodes can store data into a printed/digital image which can be read easily by our apps. In the case of 2D QR Codes, we can also encode structured data such as contact information or WiFi network credentials.

ML Kit’s Barcode Scanning API works on the device itself which makes it fast and accurate.

Firebase ML Kit Series

In this series of articles, we will deep dive into different ML Kit APIs that it offers…

Let’s look into the ML Kit’s Barcode Scanning API and how we can integrate it into our apps.

ML Kit’s Barcode Scanning

The ML Kit’s Barcode Scanning API provides the following key features.

  • The API can read almost all standard barcode formats.
  • The API by default scans for all supported barcode formats. However, you can speed up this process by explicitly specifying the formats you expect it to read.
  • The API can also parse structured data stored in 2D QR codes.
    Data such as URLs, contact information, email addresses, phone numbers, WiFi connection information etc.
QR code with WiFi information

Note: Firebase ML Kit is in beta as of January ‘19.

Let’s Code!

Step 1 : Add Firebase to your app

Offcourse! You can add Firebase to your app by following the steps mentioned here.

Step 2 : Include the dependency

You need to include the ML Kit dependency in your app-level build.gradle file.

dependencies {
// ...
implementation 'com.google.firebase:firebase-ml-vision:19.0.2'

Step 2.5 : Specify the ML models (optional)

For on-device APIs, you can configure your app to automatically download the ML models after it is installed from the Play Store. Otherwise, the model will be downloaded on the first time you run the on-device detector.

To enable this feature you need to specify your models in your app’s AndroidManifest.xml file.

<application ...>
android:value="barcode" />
<!-- To use multiple models: android:value="barcode,model2" -->

Step 3 : Get! — the Image

ML Kit provides an easy way to scan barcodes from variety of image types like Bitmap, media.Image, ByteBuffer, byte[], or a file on the device. You just need to create a FirebaseVisionImage object from the above mentioned image types and pass it to the model.

creating FirebaseVisionImage object from different image types

In my sample app I’ve used a Bitmap image to create a FirebaseVisionImage object.

val image = FirebaseVisionImage.fromBitmap(bitmap)

To create FirebaseVisionImage object from other image types, please refer to the official documentation.

Step 4 : Set! — the Model

Now, It’s time to prepare our Barcode Scanning model.

val detector = FirebaseVision.getInstance().visionBarcodeDetector

To speed up the barcode scanning process, we can also configure our model to detect only barcode formats that we expect it to read.

val options = FirebaseVisionBarcodeDetectorOptions.Builder()
val detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options)

For the full list of barcode formats that ML Kit supports, please refer to the official documentation.

Step 5 : Gooo!

Finally, we can pass our image to the model for Barcode Scanning.

.addOnSuccessListener {
Task completed successfully
.addOnFailureListener {
Task failed with an exception

Step 6 : Extract the information

Voilà! That’s it!
If the barcode scanning was successful, the success listener will receive a list of FirebaseVisionBarcode objects. Each FirebaseVisionBarcode object represents a barcode that was detected and contains all the information related to it.

You can extract all this information like this.

Have a Look!

This is what you can achieve with ML Kit’s Barcode Scanning API.

The full source-code with other ML Kit APIs can be found here!

Thanks for reading! Share this article if you found it useful.
Please do Clap 👏 to show some love :)

Let’s become friends on LinkedIn, GitHub, Facebook, Twitter.


An android developer publication to stay updated with whats new in android, best practices and how to become a better android developer

Hitanshu Dhawan

Written by

Software Engineer @UrbanClap | Google Certified Android Developer


An android developer publication to stay updated with whats new in android, best practices and how to become a better android developer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade