Read QR Code from Image File or Base64 String in Java using ZXing

Tags: Java ZXing QR Code QRCodeReader Base64 File Java QR Code

Introduction

In this tutorial, we are going to learn how to decode the QR code image to read the encoded text in a Java application. We will use ZXing open source library in order to decode the image. Via different Java code examples we show you how to extract text from an image file and from encoded base64 string image.

Add ZXing library to the project

To use ZXing Java library in the Gradle build project, add the following dependencies into the build.gradle file.

compile group: 'com.google.zxing', name: 'core', version: '3.4.1'
compile group: 'com.google.zxing', name: 'javase', version: '3.4.1'

To use ZXing Java library in the Maven build project, add the following dependencies into the pom.xml file.

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.4.1</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.4.1</version>
</dependency>

For more information about the ZXing library you can visit the library repository at github.com/zxing/zxing

Implement QR Code Reader class

In this step we implement a QRCodeReader class that can be used to read QR code from a File object, a Base64 String or a BufferedImage object.

Firstly, create a new class named QRCodeReader and implement readQRCode(BufferedImage bufferedImage) method which expects BufferedImage as argument and returns encoded text in QR Code.

public String readQRCode(BufferedImage bufferedImage) {
    String encodedContent = null;
    try {
        BufferedImageLuminanceSource bufferedImageLuminanceSource = new BufferedImageLuminanceSource(bufferedImage);
        HybridBinarizer hybridBinarizer = new HybridBinarizer(bufferedImageLuminanceSource);
        BinaryBitmap binaryBitmap = new BinaryBitmap(hybridBinarizer);
        MultiFormatReader multiFormatReader = new MultiFormatReader();

        Result result = multiFormatReader.decode(binaryBitmap);
        encodedContent = result.getText();
    } catch (NotFoundException e) {
        e.printStackTrace();
    }
    return encodedContent;
}

Next, implement the readQRCode(File qrCodeFile) method that reads QR Code from a File object.

public String readQRCode(File qrCodeFile) {
    String encodedContent = null;
    try {
        BufferedImage bufferedImage = ImageIO.read(qrCodeFile);

        encodedContent = readQRCode(bufferedImage);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return encodedContent;
}

Then, implement the readQRCode(String base64Image) method that reads QR Code image in base64 format.

public String readQRCode(String base64Image) {
    String encodedContent = null;
    try {
        byte[] imageBytes = Base64.decode(base64Image);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageBytes);
        BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);
        encodedContent = readQRCode(bufferedImage);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return encodedContent;
}

Finally, we have the complete QRCodeReader class as below.

import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

public class QRCodeReader {

    public String readQRCode(String base64Image) {
        String encodedContent = null;
        try {
            byte[] imageBytes = Base64.decode(base64Image);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageBytes);
            BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);
            encodedContent = readQRCode(bufferedImage);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return encodedContent;
    }

    public String readQRCode(File qrCodeFile) {
        String encodedContent = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(qrCodeFile);

            encodedContent = readQRCode(bufferedImage);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return encodedContent;
    }

    public String readQRCode(BufferedImage bufferedImage) {
        String encodedContent = null;
        try {
            BufferedImageLuminanceSource bufferedImageLuminanceSource = new BufferedImageLuminanceSource(bufferedImage);
            HybridBinarizer hybridBinarizer = new HybridBinarizer(bufferedImageLuminanceSource);
            BinaryBitmap binaryBitmap = new BinaryBitmap(hybridBinarizer);
            MultiFormatReader multiFormatReader = new MultiFormatReader();

            Result result = multiFormatReader.decode(binaryBitmap);
            encodedContent = result.getText();
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
        return encodedContent;
    }
}

How to read QR Code Image in Java application

In this Java code example we use the QRCodeReader class above in a Java program to read QR code from a file.

For example we have a qrcode.png file with content as below.

QR Code

import java.io.File;

public class ReadQRCodeFromFile {
    public static void main(String... args) {
        String qrCodeFileName = "qrcode.png";
        File file = new File(qrCodeFileName);

        QRCodeReader qrCodeReader = new QRCodeReader();
        String encodedContent = qrCodeReader.readQRCode(file);

        System.out.println(encodedContent);
    }
}
Output:
https://simplesolution.dev

How to read QR Code from Base64 Encoded Image in Java application

In this Java code example we use the QRCodeReader class above in a Java program to read QR code from base64 String.

public class ReadQRCodeFromBase64 {
    public static void main(String... args) {
        String base64 = "iVBORw0KGgoAAAANSUhEUgAAAGQAAABkAQAAAABYmaj5AAAA7ElEQVR42tXUsZHEIAwFUHk2cHZuQDO0QeaWTAN4twK3REYbzNAAyhww1ombvd1NbBHeMQS8CPERAH+MAn9YBWCBzAEGTcR13W8cZaEpoLdpiuA6tIb86JWhHnH1tq7vyk4l53MR3fu0p2pZzbJ8JXiqYtHP6H53uBAH3mKadpg0HRZhRrCZNBHzxnWIadBUbILRbK/KzkXxRhEHNpumMuLXLPOZ4IVoz4flA5LTlTzkO+CkqeU/Sgy65G59q92QptbXLIEZVhXQsblDlxZIy8iPDsmrIn5mdiWui/QCoKr2pq35CUPRf/nBPvUNct67nP2Y9j8AAAAASUVORK5CYII=";

        QRCodeReader qrCodeReader = new QRCodeReader();
        String encodedContent = qrCodeReader.readQRCode(base64);

        System.out.println(encodedContent);
    }
}
Output:
https://simplesolution.dev

Conclusion

In this tutorial we have implemented a Java class QRCodeReader that can be used to read QR code images in file format or in base64 String format.

Happy Coding 😊

Spring Boot Rest API Generate QR Code

Spring Boot Generate QR Code as Base64 String

Spring Boot Generate QR Code Image Files

Spring Boot Web Upload and Read QR Code Image

Spring Boot Web Generate and Display QR Code as Base64 String

Spring Boot Web Generate and Display QR Code

Generate QR Code in Java using ZXing