Reading Quick Response Code(QR) in Java example


         Before we start with reading QR code tutorial, have a look at Creating QR code tutorial in this link, which will provide an insight on QR codes.
         This tutorial explains about how to read a QR code using java. An example QR Code is shown below
Sample QR Code
Introduction :
             As said in the previous tutorial of creating QR code using java, there are many open source APIs available in the market to deal with QR codes. But the simplest and easy to use API is zxing (Zebra Crossing). To get started, we need to download zxing – ‘core’ and ‘javase’ jar files (click here to download them).
            If you are using any IDE for the development, add the downloaded jar files to the libraries folder.If you are running your application in command line, add the path of the jar files to classpath.
Once done with it, we are ready to play with QR codes. The following two simple steps explains us how to read QR code using zxing API.

Step 1: Create BinaryBitmap image from QRCode image

            To aid this task, we make use of BinaryBitmap class. This class is the core bitmap class used by ZXing to represent 1 bit data. The constructor of this class returns a binaryBitmap image by taking Binarizer as an argument.
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource( FileInputStream(filePath)))));

Note : The “filePath” in the above code snippet is the absolute path where your QR image is located. Example – “E:/QRCodes/testCode.png”

Step 2: Read content from BinaryBitmap image

              MultiFormatReader is a convenient class to decode the barcode formats. The decode method of MultiFormatReader takes the below two parameters and returns qrCodeResult of type ‘Result’. 
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap,hintMap);

binaryBitmap – the Bitmap image which we are going to decode
hintsMap –  Additional parameters to supply to the encoder. The hint type we have given is error correction. It specifies what degree of error correction to use. The one which we have used is L (L = ~7% correction).

The content is extracted from the ‘Result’ object – qrCodeResult using the below code:

import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;

public class QRCode {

public static void main(String[] args) throws WriterException, IOException,
NotFoundException {
String filePath = "E:/cte.png";
String charset = "UTF-8"; // or "ISO-8859-1"
Map hintMap = new HashMap();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

System.out.println("Data read from QR Code: "
+ readQRCode(filePath, charset, hintMap));

public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource( FileInputStream(filePath)))));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap,
return qrCodeResult.getText();

Input Image
Input image for Program
Output Screenshot
Output from Netbeans

That's it, we are done with Reading Quick Response code in java !!

Source Code of this application

1 comment:
Write comments

Popular Posts