QR-Code & Barcode Reader Android App

0
187
QR-Code Barcode Reader Android App
QR-Code Barcode Reader Android App
Say Hi! and Follow

Nowadays Barcodes and QR Codes are widely used in lot of mobile apps. In a QR Code you can store information like text, sms, email, url, image, audio and few other formats. In Android you can extract the information stored in barcodes by using Google Vision Library. Even though there are lot of other libraries available, google vision library is best to consider as it’s not only provide barcode reading but also have other features like face detection, text detection.

In this article we are going to learn how to use the google vision library by creating a simple movie ticket scanning app.

  1. Google Mobile Vision API
    Google Mobile Vision api helps in finding objects in an image or video. It provides functionalities like face detection, text detection and barcode detection. All these functionalities can be used separately or combined together.

This article aims to explain the barcode detection with a realtime use case scenario. We can see lot of barcode scanning apps used in supermarkets, theatres and hotels which scans a barcode and provides user desired information. In this article we’ll try to build a simple movie ticket scanner app which scans a barcode / qrcode and displays the movie information to book a ticket.

The google vision library is a part of play services and can be added to your project’s build.gradle.

compile ‘com.google.android.gms:play-services-vision:11.0.2’

  1. Barcode Scanner Library
    Google provided a simple tutorial to tryout the barcode scanning library with a simple bitmap image. But when it comes to scanning a realtime camera feed for a barcode, things become difficult to implement as we need to perform barcode detection on camera video.

I have developed a simple barcode scanner library by forking the google vision sample. In this library few bugs were fixed and added other functionalities like callbacks when barcode is scanned and a overlay scanning line indicator that can be used in your apps.

  1. How to Use the Barcode library
    Follow the below simple steps to include the barcode / qrcode library in your project.
  2. Add the androidhive barcode reader and google vision library to your app’s build.gradle file.

This article was written using Android Studio 3.0 Canary 9. The command compile is deprecated and replaced with implementation

build.gradle
dependencies {
// barcode reader library
implementation ‘info.androidhive:barcode-reader:1.1.5’

// google vision library
implementation 'com.google.android.gms:play-services-vision:11.0.2'

}

  1. Add the barcode camera fragment to your activity or fragment.
  1. Implement your Activity from BarcodeReader.BarcodeReaderListener and override the necessary methods.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseArray;
import com.google.android.gms.vision.barcode.Barcode;
import java.util.List;

import info.androidhive.barcode.BarcodeReader;

public class MainActivity extends AppCompatActivity implements BarcodeReader.BarcodeReaderListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
}

@Override
public void onScanned(Barcode barcode) {
    // single barcode scanned
}

@Override
public void onScannedMultiple(List<Barcode> list) {
    // multiple barcodes scanned
}

@Override
public void onBitmapScanned(SparseArray<Barcode> sparseArray) {
    // barcode scanned from bitmap image
}

@Override
public void onScanError(String s) {
    // scan error
}

@Override
public void onCameraPermissionDenied() {
    // camera permission denied
}

}

  1. Run your project and try to scan barcode or qrcode. The scanned result will be returned in onScanned() or onScannedMultiple() method.

3.1 Adding Scanning Overlay Indicator Line
We can see all the scanning apps generally adds an indicator line on the camera overlay to indicate the scanning progress in going on. To achieve this, I have added a reusable class in the same library which can be added on to camera screen.

To add the animating scanning line, add the info.androidhive.barcode.ScannerOverlay to same activity overlapping the camera fragment.


The library also contains few other useful functionalities like auto flash, beep sound etc., Detailed information about library can be found on it’s Github page.

  1. Creating New Project – Building Simple Ticket Scanning App
    As the barcode scanning library is already available, we’ll see how to use the library considering a real use case scenario. The app we are going to build not only explains the barcode scanning, but it also covers building the complex UI, making REST api calls to fetch the movie json and writing custom view classes.

Overall the app contains three screens. The first is splash screen, second is barcode scanning and the last one is to display the movie ticket information.

Below are the screenshots of the app.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 25,221 other subscribers

Leave a Reply