Generating QR Codes in Java with ZXing (Zebra Crossing)
QR codes have become an integral part of the modern web and mobile applications. They allow you to quickly transmit links, contact information, text data, and much more. In the Java world, one of the most popular and stable libraries for working with QR codes is ZXing (Zebra Crossing).
In this article, we will thoroughly examine how to add ZXing to your project, create a simple QR code, customize its appearance, and save it to a file. The material will be useful for both beginners and experienced developers who want to add QR code generation functionality to their Java applications.
What is ZXing?
ZXing (pronounced "zebra crossing") is an open-source library for processing one-dimensional and two-dimensional barcodes. It is written in Java and supports many formats: QR Code, Data Matrix, Aztec, PDF417, and others. Key features:
- Generation (creation) of QR codes from text, URLs, contacts, etc.
- Reading (decoding) QR codes from images.
- Customization of color, size, and error correction level.
- Output to various graphic formats (PNG, JPEG, SVG).
- Support for Java SE, Android, and other JVM languages (Kotlin, Scala).
The library is actively maintained by the community and is used in thousands of projects worldwide.
Adding ZXing to a Project
The easiest way to add ZXing to your project is to use the Maven or Gradle build system. If you are using Maven, add the following dependency to your pom.xml file:
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.5.3</version></dependency><dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.5.3</version></dependency>For Gradle (build.gradle):
implementation 'com.google.zxing:core:3.5.3'implementation 'com.google.zxing:javase:3.5.3'The core module contains the main classes for working with codes, and javase provides helper utilities for Java SE (e.g., for working with images).
Creating a Simple QR Code
Let's look at a basic example: generating a QR code containing the text string "Hello, PythonLib!".
import com.google.zxing.BarcodeFormat;import com.google.zxing.WriterException;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.QRCodeWriter;
import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;
public class QRCodeGenerator { public static void main(String[] args) { String text = "Hello, PythonLib!"; int width = 300; int height = 300;
QRCodeWriter qrCodeWriter = new QRCodeWriter(); try { BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
BufferedImage qrImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { qrImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } }
File outputFile = new File("qrcode.png"); ImageIO.write(qrImage, "png", outputFile); System.out.println("QR code saved to file: " + outputFile.getAbsolutePath());
} catch (WriterException | IOException e) { e.printStackTrace(); } }}This code creates a black-and-white QR code of 300x300 pixels and saves it to a file named qrcode.png.