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.
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.
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…
compile 'com.google.android.gms:play-services-vision:9.8.00'
<meta-dataandroid:name="com.google.android.gms.vision.DEPENDENCIES"android:value="barcode"/>
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();
if (!barcodeDetector.isOperational()) {new AlertDialog.Builder(this).setMessage("Barcode detector could not be set up on your device :(").show();return;}
Frame frame = new Frame.Builder().setBitmap(barcodeBitmap).build();SparseArray<Barcode> barcode = barcodeDetector.detect(frame);
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);}
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”.