How to Easy Add QR Code and Bar Code Scanning Functionality in Android Application

Hello all, Today we will learn about integrating Bar Code and Qr Code functionality in our Android app, Generally many on line shopping, bill pay, security, offer redeem applications provides this kind of functionality. Main benefit of using this feature to reduce the user input error in system, As these codes contains fix data which we can use directly without end user interaction using scanner. In this tutorial we will learn how to integrate this feature in our application easily with just few steps.


First of all add this gradle dependency in our application’s build.gradle file.

compile 'me.dm7.barcodescanner:zxing:1.9.8'

After that we need to add Camera permission in our application’s AndroidManifest.xml file. But for Android devices running version 6.0 and above we need to implement Android Runtime Permission Request for CAMERA as mentioned here.

<uses-permission android:name="android.permission.CAMERA" />

After that we can use scanner in our Activity file as shown in below example code

public class MyScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView myScannerView;
public void onCreate(Bundle state) {
myScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
setContentView(myScannerView);                // Set the scanner view as the content view
public void onResume() {
myScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
myScannerView.startCamera();          // Start camera on resume
public void onPause() {
myScannerView.stopCamera();           // Stop camera on pause
public void handleResult(Result rawResult) {
// Do something with the result here
Log.v(TAG, rawResult.getText()); // Prints scan results in logcat window
Log.v(TAG, rawResult.getBarcodeFormat().toString()); // Prints the scan format of code (datamatrix, qrcode, pdf417, upca/upcn etc.)
// If you like to resume scanning, call this method below or send result data to another view/web service/activity from here:

In above case scanner will open in full activity as Scanner is directly added to setContentView in OnCreate of above Activity. But if you would like to add Scanner View in your existing activity Just add a FrameLayout in your Activity or Fragment as below and then programmatically add Scanner View to it.

android:layout_height="wrap_content" />

layout_width and layout_height you can set according to your screen layout and add Scanner to this layout as below.

ViewGroup contentFrame = (ViewGroup) findViewById(;
myScannerView = new ZBarScannerView(this);//or getActivity() in case of Fragment

Other methods including life cycle methods should remain same as mentioned in above Activity.

Below are some methods for extra functionality is available to work with myScannerView object but that may depend on devices.

To enable Flash Mode in supported devices.

// Toggle flash
void setFlash(boolean);
myScannerView.setFlash(true); //To On Flash
myScannerView.setFlash(false); //To Off Flash

To enable Auto Focus in supported devices.

// Toogle autofocus:
void setAutoFocus(boolean);
myScannerView.setAutoFocus(true); //To Enable Auto Focus
myScannerView.setAutoFocus(false); //To Disable Auto Focus

To Specify Limited Format Supported. As by default it will support all listed formats.

// Specify interested barcode formats:
void setFormats(List<BarcodeFormat> formats);
List<BarcodeFormat> formats = new ArrayList<>();

Below is the list of all BarcodeFormat types supported.


To Specify Particular Camera to use. As by default it will use device back camera.

// Specify the cameraId to start with: 0 or 1
void startCamera(int cameraId);
myScannerView.startCamera(0); //For Device Back Camera
myScannerView.startCamera(1); //For Device Front Camera


For some device specific problem in HUAWEI devices like P9, P10 it won’t work due to the “preview size” error. Please add this additional parameter as below:

// this parameter is for HUAWEI devices only! But works with other devices also.

As always kindly let us know in comments below if you have any problem in implementation or you have any suggestion regarding tutorial.

Code Credit –

(Visited 8 times, 1 visits today)