barcode_qrcode_zxing

Generate or Read Bar Code & Qr Code in Android using Zxing lib


In this tutorial, we’ll use the ZXing (Zebra Crossing) library to generate or read barcode & qrcode within an Android app.

QR code (abbreviated from Quick Response Code) is the trademark for a type of matrix barcode (or two-dimensional barcode) first designed for the automotive industry in Japan. Bar codes are optical machine-readable labels attached to items that record information related to the item. Initially patented, its patent holder has chosen not to exercise those rights. Recently, the QR Code system has become popular outside the automotive industry due to its fast readability and greater storage capacity compared to standard UPC barcodes.

The code consists of black modules (square dots) arranged in a square grid on a white background. ZXING is a Multi-format 1D/2D barcode image processing library with clients for Android, Java. It is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages.

 

Getting ZXing Library to our project

Add the following line to build.gradle file in your Android Studio project:

Taking a Glimpse of ZXing Library

Here are the 1D/2D barcode writers. All barcode formats are defined in BarcodeFormat.java:

The class MultiFormatWriter has covered all supported barcode writers. Instead of any specific bar code writer, we just need to use MultiFormatWriter with a specified barcode format.

Bar Code/ QR Code Writer and Reader

Writing Bar Code/ QR Code:

Create a new instance of MultiFormatWriter with the content you need to embed, type of Barcode format along with width and height required.

With the return result BitMatrix, create a new bitmap with the required dimensional and colours.

 

Complete Method and Usage:

For Bar Code:

For Qr Code:

 

Reading Bar Code/ QR Code:

Initiating the Scanner

Create an instance of the Intent Integrator class and we can call on the Intent Integrator method to start scanning.

Adding  additional properties for the scanner.

Inside the onActivityResult, try to parse the result into an instance of the ZXing Intent Result class.

The Intent Result object provides methods to retrieve the content of the scan and the format of the data returned from it. Retrieve the content as a string value.

 

Complete Scanner Code:

 

Screen Shots:

zxing_homezxing_qrcodezxing_barcode
zxing_scanzxing_scan_result

 

Source Code:

Complete Source Code available at here.

I hope you will like the article and it will definitely help you to make your apps more productive. In case of any confusion or query, feel free to ask. And if you found this tutorial to be useful, Share with others.


About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.