In the previous post, we learned how to generate QR Codes in java using google’s Zxing library. If you have not read my previous tutorial, I encourage you to have a look at that first before reading this one.

In this post we’ll learn how to read QR code images and extract the data encoded inside the QR Code.

QR Code Reader

Scan the above QR code using your smartphone. You’ll get my site’s url - http://callicoder.com. We’ll write a similar scanner in Java where you can pass a QR code image, and the program will return the data encoded in the QR code.

Reading QR Code image in Java

We’ll use Google’s zxing library to read QR code images.

Please make sure that following zxing dependencies are added in your pom.xml file -

<!-- For Maven Users -->
<dependencies>
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>core</artifactId>
        <version>3.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>javase</artifactId>
        <version>3.3.0</version>
    </dependency>
</dependencies> 

If you’re using gradle then add the following dependencies -

# For Gradle users
compile "com.google.zxing:core:3.3.0"
compile 'com.google.zxing:javase:3.3.0'

If you’re not using any build system, then you can add the following zxing jars directly in the classpath -

  1. zxing core-3.3.0.jar

  2. zxing javase-3.3.0.jar

Program to Read QR Code image

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class QRCodeReader {

    private static String decodeQRCode(File qrCodeimage) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(qrCodeimage);
        LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        try {
            Result result = new MultiFormatReader().decode(bitmap);
            return result.getText();
        } catch (NotFoundException e) {
            System.out.println("There is no QR code in the image");
            return null;
        }
    }

    public static void main(String[] args) {
        try {
            File file = new File("MyQRCode.png");
            String decodedText = decodeQRCode(file);
            if(decodedText == null) {
                System.out.println("No QR Code found in the image");
            } else {
                System.out.println("Decoded text = " + decodedText);
            }
        } catch (IOException e) {
            System.out.println("Could not decode QR Code, IOException :: " + e.getMessage());
        }
    }
}

In the above program, the decodeQRCode() function takes an image file and tries to read any QR code in the image. If a QR code is found, it returns the text otherwise it returns null.

Conclusion

In this blog post, We learned how to read QR codes in Java using Zxing library. You can find all the code samples in my github repository.

Zxing library has several other useful features that you can use. For example, reading multiple QR codes from the image using MultipleBarcodeReader.

Checkout Zxing Github page for any help with the library.

Also, don’t forget to explore Zxing online QR code generator and decoder applications -

Thank you for reading. Please ask any questions or doubts in the comment section below.