Generate QR Code in Java using ZXing

Tags: Java ZXing QR Code Java QR Code

Introduction

In this tutorial, we are going to learn how to generate QR code (abbreviated from Quick Response code) in a Java application. We will use ZXing (“zebra crossing”) library which is the most popular library to generate QR code in Java.

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

How to Generate QR Code in Java

To generate QR Code using ZXing we need to instantiate a QRCodeWriter object.

QRCodeWriter qrCodeWriter = new QRCodeWriter();

QRCodeWriter class provides the encode() method to generate code and return a BitMatrix object.

BitMatrix bitMatrix = qrCodeWriter.encode(contents, BarcodeFormat.QR_CODE, width, height);

Then from the BitMatrix object we can write QR code to the file system using MatrixToImageWriter.writeToPath() static method.

MatrixToImageWriter.writeToPath(bitMatrix, "PNG", filePath);

Following example Java code show you ho to generate the QR code with content https://simplesolution.dev into file qrcode.png.

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class QRCodeExample {
    public static void main(String... args) {
        try {
            String contents = "https://simplesolution.dev";
            String fileName = "qrcode.png";
            int width = 100;
            int height = 100;
            
            // generate QR code
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            BitMatrix bitMatrix = qrCodeWriter.encode(contents, BarcodeFormat.QR_CODE, width, height);

            // write to file
            Path filePath = Paths.get(fileName);
            MatrixToImageWriter.writeToPath(bitMatrix, "PNG", filePath);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Execute the Java program then we get the qrcode.png file as below.

QR Code

Happy Coding 😊

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

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