How to detect QR code(2D bar code) in an image using Zxing library

Here I am going to discuss about scanning of QR code in an Image. First I would like to introduce you about QR.

What is QR?

A QR code (abbreviated from Quick Response code) is a specific matrix barcode (or two-dimensional code) that is readable by dedicated QR barcode readers and camera telephones.

The code consists of black modules arranged in a square pattern on a white background. The square patterns are encoded information that may be text, URL, or other data.

What is ZXing library ?

ZXing (pronounced “zebra crossing”) is an open-source, multi-format 1D/2D barcode image processing library implemented in Java.

To know more please visit: code.google.com/p/zxing/

Now I proceed to the main subject of this tip.

Here I am assuming that I have an image named “Secure.jpg” which may contain a 2D barcode at any location of image.

Our aim is to scan “Secure.jpg” image and want to know whether this image contains a 2D bar code or not.

We need to follow simple 4 steps to detect QR using ZXing library.

We need to follow simple 4 steps to detect QR using ZXing library.

So lets proceed now:-

Step 1: Convert “Secure.jpg” into BitMatrix form. BitMatrix represents a 2D matrix of bits. In this step you will get data of Secure.jpg image in 2D matrix of bits form as BitMatrix object.

BitMatrix is a class of ZXing library.

Step 2: Now we have 2D matrix of bits in form of BitMatrix object from step 1. Need to create an object of MultiDetector(A class of ZXing library) by passing this BitMatrix object in constructor of MultiDetector.

Suppose from step 1 we get BitMatrix object of name “imgBitMatrixObj”.

MultiDetector detector = new MultiDetector(imgBitMatrixObj);

Step 3: In this step you need to detect the Secure.jpg by calling detect() method of MultiDetector class. This detect() method returns you an object of DetectorResult (A class of ZXing library) class.

If your image “Secure.jpg” containing any 2D barcode it will return you the data of QR image (2D bar code) in bits form wrapped as DetectorResult object otherwise it will return null.

DetectorResult dResult = detector.detect();

Step 4: Now just check this dResult whethet it is null or not.
Case 1: dResult != null means "Secure.jpg" containing a 2D bar code (QR image)

Case 2: dResult == null means "Secure.jpg" does not contain any 2D bar code.

Now obviously you were thinking yes we detected that this image has QR code but how I will get that QR code(2D bar code )? Since we will have requirement to process that 2D bar code and get all the information stored in QR.

For this there is a method "getBits()" in DetectorResultclass. Ths method returns you QR image in bits as BitMatrix object.

BitMatrix QRImageData = dResult.getBits();

Now you have QR image in BitMatrix form. You need to decode the QR image and then you can get hidden info under QR.

I will share with you the process of decoding the QRImage using ZXing library in another TIP.

I hope it would be helpful for those who have never worked with QR Image processing in java using ZXing library.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!