Machine Learning for Android Developers with the Mobile Vision API — Part 2— Barcode Detection by@moyinoluwa

Machine Learning for Android Developers with the Mobile Vision API — Part 2— Barcode Detection

Moyinoluwa Adeyemi Hacker Noon profile picture

Moyinoluwa Adeyemi

Android Dev

I recently started exploring Machine Learning for Android Developers with the Mobile Vision API. I wrote about the Face Detection API in the first post in this series and I’m going to continue with the Barcode Detection API in this article.

Barcode Detection API

The Barcode Detection API is used to process barcodes locally on the device (an Android device in this case) and in any orientation. According to the Barcode API Overview, there are two major barcode formats; the 1D and 2D formats. Most of the available barcodes can be classified into these formats. The API allows for the reading of the two different barcode formats and automatically parses them for values. The 1D barcodes contain a number embedded in the barcode while the 2D barcodes contain structured data which can include URLs, Contact information, Calendar events, Email, Phone SMS, ISBN, WiFi, Geo-location and AAMVA driver license/ID.

Getting Started

In this article, just like the first article, we are going to walk through the process of creating a very basic application which parses a preloaded image with the Barcode Detection API for information. The code is on Github although this initial version doesn’t contain tests or follow any specific architecture. Yet.

Here we go…

  • Create a new project in Android Studio.
  • Import Google Play Services SDK for the Mobile Vision API into your app level build.gradle file. As at the time of writing this article, the latest version is 9.8.00. As usual, you are bound to hit the 65k method limit if you import the whole SDK instead of the specific one (play-services-vision) you need.
compile ''
  • To enable the vision dependencies to be automatically installed for barcode detection, add this meta-data to the manifest file. This ensures the appropriate libraries are downloaded in time for first time users of the app.
  • We’ll create a very simple layout consisting of a Button, an ImageView and a TextView. The ImageView displays the barcode from the drawable folder (This is just for the purpose of this tutorial only. A real-life scenario will most likely involve detecting the barcode through the device camera). The button starts the processing of the image while the TextView displays whatever data is gotten off of the image.
  • Like the Face Detection API, the image has to be converted into a Bitmap to be processed. But unlike the Face Detection API, there’s no need to make the Bitmap mutable (at least in this article) since all we want to do here is display the date from the barcode in another view.
Bitmap barcodeBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.barcode_image);

As earlier mentioned, a click of the button starts the processing of the barcode now in the ImageView. We’ll proceed to do that here with the Barcode Detection API. We can set the barcode formats we want the API to keep an eye out for. This can include any one of the 1D or 2D formats. You can have as many formats as you want e.g. (Barcode.SMS | Barcode.DRIVER_LICENSE | Barcode.ISBN) but to cover all the bases in this article, we’ll just look out for all the formats.

BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this)
  • Next, we need to check if the barcode detector is operational already. There’s always the possibility that it won’t work the first time because a library needs to be downloaded to the device and it might not have been completed in time for use.
if (!barcodeDetector.isOperational()) {
new AlertDialog.Builder(this)
.setMessage("Barcode detector could not be set up on your device :(")
  • We then create a frame using the barcode bitmap and call the barcode detector.
Frame frame = new Frame.Builder().setBitmap(barcodeBitmap).build();
SparseArray<Barcode> barcode = barcodeDetector.detect(frame);
  • Our results are now contained in the SparseArray. The array can either be empty or contain values depending on how successful the parsing was or if there was even a barcode in the image. It’s safer to perform checks to catch such bugs. The next step is to get the UI ready to display the values from the array. In a real app, I would probably display the results in a RecyclerView or add more TextViews programatically. All I’m doing here is to display all the values in one TextView.
if (size == 0) {
textView.setText("No information available");
} else {
for (int i = 0; i < size; i++) {
barcodeValue += (barcode.valueAt(i).displayValue + "\n");
  • As good Android citizens, we should also remember to release the barcode detector once the transaction is completed.
Go on ahead and find out what’s embedded in this barcode. I believe in you! :D

That’s all for this article, but I only just skimmed the surface. It’s possible to detect barcodes through the camera of the device instead of through default images. You can have multiple barcodes in one image have your app detect them even if they are upside down. You can parse drivers licenses, allow people connect automatically to your WiFi and exchange business cards all via this API. The code from this article is on Github here.

If you figure out more cool ways to use this API, please leave a comment below.


Thought this was great? Please don’t forget to “Recommend” and “Share”.


Join Hacker Noon

Create your free account to unlock your custom reading experience.