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;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
myScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
setContentView(myScannerView);                // Set the scanner view as the content view
}
@Override
public void onResume() {
super.onResume();
myScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
myScannerView.startCamera();          // Start camera on resume
}
@Override
public void onPause() {
super.onPause();
myScannerView.stopCamera();           // Stop camera on pause
}
@Override
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:
myScannerView.resumeCameraPreview(this);
}
}

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.

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="wrap_content"
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(R.id.content_frame);
myScannerView = new ZBarScannerView(this);//or getActivity() in case of Fragment
contentFrame.addView(myScannerView);

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);
//Usage
myScannerView.setFlash(true); //To On Flash
myScannerView.setFlash(false); //To Off Flash

To enable Auto Focus in supported devices.

// Toogle autofocus:
void setAutoFocus(boolean);
//Usage
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);
//Usage
List<BarcodeFormat> formats = new ArrayList<>();
formats.add(BarcodeFormat.UPC_A);
.
.
formats.add(BarcodeFormat.QR_CODE);
.
.
formats.add(BarcodeFormat.PDF_417);
.
.
etc.
myScannerView.setFormats(formats);

Below is the list of all BarcodeFormat types supported.

BarcodeFormat.UPC_A
BarcodeFormat.UPC_E
BarcodeFormat.EAN_13
BarcodeFormat.EAN_8
BarcodeFormat.RSS_14
BarcodeFormat.CODE_39
BarcodeFormat.CODE_93
BarcodeFormat.CODE_128
BarcodeFormat.ITF
BarcodeFormat.CODABAR
BarcodeFormat.QR_CODE
BarcodeFormat.DATA_MATRIX
BarcodeFormat.PDF_417

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);
//Usage
myScannerView.startCamera(0); //For Device Back Camera
myScannerView.startCamera(1); //For Device Front Camera

Edit:

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.
mScannerView.setAspectTolerance(0.5f);

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 – https://github.com/dm77/barcodescanner

(Visited 8 times, 1 visits today)