paint-brush
Machine Learning for Android Developers with the Mobile Vision API — Part 2— Barcode Detectionby@moyinoluwa
4,228 reads
4,228 reads

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

by Moyinoluwa AdeyemiOctober 24th, 2016
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

I recently started exploring Machine <a href="https://hackernoon.com/tagged/learning" target="_blank">Learning</a> for <a href="https://hackernoon.com/tagged/android" target="_blank">Android</a> Developers with the Mobile Vision API. I <a href="https://hackernoon.com/machine-learning-for-android-developers-with-the-mobile-vision-api-part-1-face-detection-e7e24a3e472f#.sc2kfcbai" target="_blank">wrote about the Face Detection API</a> in the first post in this series and I’m going to continue with the Barcode Detection API in this article.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Machine Learning for Android Developers with the Mobile Vision API — Part 2— Barcode Detection
Moyinoluwa Adeyemi HackerNoon profile picture

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 'com.google.android.gms:play-services-vision:9.8.00'

  • 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.



<meta-dataandroid:name="com.google.android.gms.vision.DEPENDENCIES"android:value="barcode"/>

  • 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).setBarcodeFormats(Barcode.ALL_FORMATS).build();

  • 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 :(").show();return;}

  • 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");}textView.setText(barcodeValue);}

  • As good Android citizens, we should also remember to release the barcode detector once the transaction is completed.

barcodeDetector.release();

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.

References:


Track Faces and Barcodes | Mobile Vision | Google Developers_This page is a walkthrough of how to build an app that uses the rear facing camera to show a view of the detected faces…_developers.google.com


Barcode Detection with the Mobile Vision API_With the release of Google Play services 7.8 we're excited to announce that we've added new Mobile Vision APIs which…_codelabs.developers.google.com


moyheen/barcode-detector_barcode-detector - This application contains all the code from my article on the Barcode Detector API. https:…_github.com

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