Android Example – Programmatically Scan QR Code and Bar Code

Android Scan QR Code Example

Often when building Android apps, we encounter situations where it is required to scan a bar code or QR code. Now as difficult as it sounds, the complex code it has if you plan to write your own scanning algorithm. But this is the place where new and improved Mobile Vision from Google steps in. This API was initially introduced in August 2015, but now it as been further improved and re-introduced including features like OCR and object tracking along with the existing features of scanning qr code and barcodes. Therefore here lets understand the usage of Google’s Mobile Vision with an example where we scan a QR code on Android.

Programmatically scanning a QR Code or Bar Code on Android has always been a topic of great discussion. There are many ways through which this can be done:

  • Using a web based API solution, where image is uploaded to a server and it returns back the results.
  • Use a hybrid solution where web based app accesses your camera and scans Bar Code or QR Code.
  • Write your own image scanning algorithm.
  • Use the Google Play Services integrated Mobile Vision.

In my opinion using the Android Mobile Vision for scanning QR codes is the most favorable option in comparison to other options because it offers offline scanning of images, resulting a fast scan and an immediate result. This being said its also the easiest option available. As it just requires a few lines of code to make it work. Besides that; when using this library, you don’t need to import any other android scanning library as this API also takes care of OCR and facial scanning. To read more about it please visit my article on Android Mobile Vision API (coming soon).


Introducing the Official Android QR and Barcode Scanning Library : Mobile Vision

There are many third party libraries available for parsing of barcodes and QR codes, some of them are even endorsed by Google I believe. But nowdays reading of QR codes and barcodes on Android has become so common that, it demanded an official solution. Hence the Mobile Vision APIs were introduced with QR code and barcode scanning capabilities for Android. While most of the opensource libraries scan only one QR code or barcode at a time. These Mobile Vision APIs can scan multiple QR codes at the same time. Also if you want, you can directly integrate them with android’s camera and scan for barcodes and QR codes on the fly while streaming on a surface.

As of now Mobile Vision APIs can scan following types of Bar codes and QR codes on Android:


Setting up Android QR Code Scan Library : Mobile Vision

As I have mentioned before, setting up Mobile Vision API is very easy, all of its downloads are managed internally by Google Play services. Although to properly integrate it and make it functional before the actual usage, it is advised to reference the used features of Mobile Vision API in the Manifest of your app. This way the dependencies are downloaded before you can actually use them i.e. at the time of installation. Also please don’t forget to follow the steps for Google Play Services Integration. But in our case we only need to compile, therefore make sure your build.gradle (app) has following dependencies:

And build.gradle(global) should have this:

Also as mentioned to make Mobile Vision APIs functional for an Android Scan QR Code Example before installation, please include these lines in your Manifest inside the application tag:

Here the android:value tag can have following comma separated values:

  • barcode
  • face
  • ocr


Android Scan QR Code Example

One of the interesting features of Mobile Vision API is that it performs a scan very fast. Also its documentation states that you can perform multiple types of scans on the same frame. This means you can scan multiple barcodes, QR Codes and faces together. Be it a video, a picture, or a video stream. But covering all of this in a single article is not possible. Therefore to start off we will build a basic example where we would use native phone’s camera to take a picture and scan it for barcodes and qr codes, and building a basic Android Scan QR code Example.

To start off, have a look at the Manifest, where we define the camera and barcode dependency for this Android Scan QR Code example:

Please Note: full source code is available at the end of the tutorial.

Next lets define a basic layout to show the QR code scan results.


Now lets define the main activity where the Android QR/Bar code scanning takes place:

Please Note: Images to be scanned, should not be too big.

When it comes to scanning of QR code from a picture on an Android app using the Mobile Vision APIs, the process is quite simple. All we need to do is, pass on a Frame to the BarcodeDetector, this will return an array of type Barcode, which would contain the actual barcode value. But there are prerequisites to this implementation, like declaring all permissions and getting them on run time, which is shown in the android scan qr code example above. To view full source code for this example please refer to the link below:

Full Source Code


Also please have a look at the screens for this Android Scan QR code scan example:

This slideshow requires JavaScript.

By using these Mobile Vision APIs for barcodes and QR codes, we can also determine the type of barcode or QR code is being scanned. This is one of the most interesting parts about this API, it reads the data, as well as it understands what type of data it is. For e.g. when an event QR code is scanned, we can use Barcode.CalendarEvent class to extract out the exact data in required format. This can help us in reducing our code bases very efficiently.


When speaking of Android Barcode scanning libraries, Mobile Vision API is the latest addition in the list. Also it is one of the most stable and reliable options as its a library provided by Google, through play services. Which definitely gives it an added advantage over other libraries. That being said, would like to say, we have finally succeeded in offline scanning of barcodes and QR Codes. As most of the methods require uploading of images to a server, or including huge code bases or libraries into our project for offline scanning. But with Mobile Vision APIs nothing of this sort needs to be done, as all this is taken care by Google Play Services once they are downloaded. Hope this helped you. Connect with us on Twitter, Facebook and Google+ for more updates.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.

Leave a comment

Your email address will not be published. Required fields are marked *

8 thoughts on “Android Example – Programmatically Scan QR Code and Bar Code

  • Matt Faust

    so I have followed your tutorial to add a barcode scanner to my app and have a issue I can not figure out. Could you please help?
    In the xml file..

    android:src=”@mipmap/truiton” is not found, so I set it to a image I did have in my project, I am able to take a picture but it never recognizes a barcode …..should not the ImageView src be a live preview of the camera? I do not know how to do that am I’m stuck…

    • Mohit Gupt Post author

      Hi, You can safely remove the whole ImageView, as the picture would be taken from your device’s camera.

      Also if it is not recognizing your barcode, then please ensure that your barcode falls under the specified list of supported barcode and QR code types.

      • Richard

        Hello. It doesn’t recognizing my barcode, I have tried multiple QR codes which should fall under the specified list. I am using play-service 10.2 now. Thank you.

  • Paco Andrade

    This actually dont work, i try this and the Ocr and it always say could not setup the detector, i try with 3 different phones and is always the same!! :/