Generating QR Codes in Java with ZXing
QR codes have become an integral part of the modern web and mobile applications. They are used for quickly transmitting links, contact information, text, and other data. In Java, one of the most popular and stable libraries for working with QR codes is ZXing ("Zebra Crossing"). In this article, we will cover how to add ZXing to a project, generate a QR code, and save it to a file.
What is ZXing?
ZXing (Zebra Crossing) is an open-source library for processing one-dimensional and two-dimensional barcodes, written in Java. It supports reading and writing QR codes, Data Matrix, PDF417, and many other formats. The library is actively maintained by the community and is used in thousands of projects.
Adding the Library
To work with ZXing in a Java project (e.g., Maven or Gradle), you need to add two main dependencies:
core— contains the base classes for working with codes.javase— contains classes for generating images (PNG, JPEG) and working with AWT.
Example for Maven (pom.xml):
<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, add to build.gradle:
implementation 'com.google.zxing:core:3.5.3'implementation 'com.google.zxing:javase:3.5.3'Basic Example: Generating a QR Code
Let's create a simple method that takes text and a file path, then generates a QR code of size 300x300 pixels.
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.FileSystems;import java.nio.file.Path;
public class QRCodeGenerator {
public static void generateQRCode(String text, String filePath) throws WriterException, IOException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, 300, 300);
Path path = FileSystems.getDefault().getPath(filePath); MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); }
public static void main(String[] args) { try { generateQRCode("https://pythonlib.ru", "qrcode.png"); System.out.println("QR code successfully created!"); } catch (WriterException | IOException e) { System.err.println("Error generating QR code: " + e.getMessage()); } }}After running, a file qrcode.png will appear in the project root with a QR code containing the link to https://pythonlib.ru.
Customizing Color and Size
By default, the QR code is generated in black and white. You can change the foreground and background colors using MatrixToImageConfig:
import com.google.zxing.client.j2se.MatrixToImageConfig;
public static void generateColoredQR(String text, String filePath) throws WriterException, IOException { QRCodeWriter writer = new QRCodeWriter(); BitMatrix matrix = writer.encode(text, BarcodeFormat.QR_CODE, 400, 400);
// Foreground color (blue), background color (yellow) int foregroundColor = 0xFF3366FF; // ARGB int backgroundColor = 0xFFFFFF00; MatrixToImageConfig config = new MatrixToImageConfig(foregroundColor, backgroundColor);
Path path = FileSystems.getDefault().get Also in library
Image Processing in C++: resize and crop with the OpenCV library
Alamofire for Swift: Complete Guide with Examples
OpenSSL C++ Guide: Complete Tutorial with Code Examples