Android Bar Code/ QR-Code Scanner Tutorial using ZXING Scanner Library

This tutorial explains how to implement Bar Code/ QR- Code Scanner using ZXING library in Android device.


barcode scanner

Introduction

Quick Response(QR) code is a two dimensional barcode(matrix codes) that allows content to be decoded at a high speed. The use of QR code is license-free. This tutorial explains how to implement ZXING library to scan or decode Bar Code. This tutorial creates a Scanner by using Camera and decode Bar Code.

Create a new project

To implement Bar Code scanner, we are going to create a new android project. Go to File ⇒ New ⇒ New Projects in Android studio.

Add ZXING library

We need to add ZXING library in our project to implement scanner, so open build.gradle(Module:app) file and add following code:

repositories{
  maven{
    url "https://jitpack.io"
  }
}

dependencies{
   ..... 
  compile 'me.dm7.barcodescanner:zxing:1.9'
  .....
}

Initialize Scanner View

To decode Bar code, we need to implement a scanner. We will initialize ZXingScannerView in the MainActivity.java.

public class MainActivity extends AppCompatActivity {
    private ZXingScannerView mScannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Programmatically initialize the scanner view
        mScannerView = new ZXingScannerView(this);   
        setContentView(mScannerView);
    }
}

Implement ResultHandler

To get the scanned result of QR Code, we will implement the ZXingScannerView.ResultHandler class. This class will handle the result by using the handleResult() method. So, we need to add this method in the MainActivity.java file. The handleResult() gets the result and displays it in the alert dialog box.

public class MainActivity extends AppCompatActivity 
                      implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Programmatically initialize the scanner view
        mScannerView = new ZXingScannerView(this);   
        setContentView(mScannerView);
    }

    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        Log.v("TAG", rawResult.getText()); // Prints scan results
        // Prints the scan format (qrcode, pdf417 etc.)
        Log.v("TAG", rawResult.getBarcodeFormat().toString()); 
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Scan Result");
                builder.setMessage(rawResult.getText());
                AlertDialog alert1 = builder.create();
                alert1.show();

        // If you would like to resume scanning, call this method below:
        mScannerView.resumeCameraPreview(this);
    }
}

Register Handler for Scan Result

We will register ourselves as a handler for scan results and start camera on Resume and stop on Pause.

public class MainActivity extends AppCompatActivity 
                      implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Programmatically initialize the scanner view
        mScannerView = new ZXingScannerView(this);   
        setContentView(mScannerView);
    }

    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        Log.v("TAG", rawResult.getText()); // Prints scan results
        // Prints the scan format (qrcode, pdf417 etc.)
        Log.v("TAG", rawResult.getBarcodeFormat().toString()); 
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Scan Result");
                builder.setMessage(rawResult.getText());
                AlertDialog alert1 = builder.create();
                alert1.show();

        // If you would like to resume scanning, call this method below:
        mScannerView.resumeCameraPreview(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        // Register ourselves as a handler for scan results.
        mScannerView.setResultHandler(this); 
        // Start camera on resume
        mScannerView.startCamera();          
    }

    @Override
    public void onPause() {
        super.onPause();
        // Stop camera on pause
        mScannerView.stopCamera();
    }
}

 





 


About Abhay Anand

Abhay Anand has experience in the field of Software Development. He can program Android, PHP, JavaScript, Html, Css. He develops web and Android applications. You can reach him at anand.abhay1910@gmail.com