QrCodeUtil
Origin
Due to the high demand for QR codes, I believe that the generation and parsing of QR codes should exist as a simple tool in Hutool. Considering the difficulty of implementing it on our own, Hutool encapsulates the widely accepted zxing library. As it involves a third-party package, it is categorized into the extra module.
Usage
Introducing zxing
Taking into account Hutool’s non-mandatory dependency, zxing needs to be introduced by the user:
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
Note: zxing-3.3.3 is the latest version as of this documentation, and the version you introduce should be consistent with or newer than this version.
Generating QR Code
Here we will generate a QR code for the Hutool homepage URL. You can scan it with WeChat to see the H5 homepage:
// Generate a QR code for the specified URL to a file, with a width and height of 300 pixels
QrCodeUtil.generate("https://hutool.cn/", 300, 300, FileUtil.file("d:/qrcode.jpg"));
The resulting qrcode.jpg:
Customizing Parameters (since 4.1.2)
- Basic Parameter Settings
You can customize the generation parameters of the QR code through QrConfig
, such as length, width, color of the QR code, background color, margin, etc. Here’s how to use it:
QrConfig config = new QrConfig(300, 300);
// Set the margin, which is the margin between the QR code and the background
config.setMargin(3);
// Set the foreground color, which is the color of the QR code (cyan)
config.setForeColor(Color.CYAN.getRGB());
// Set the background color (gray)
config.setBackColor(Color.GRAY.getRGB());
// Generate the QR code to a file or a stream
QrCodeUtil.generate("http://hutool.cn/", config, FileUtil.file("e:/qrcode.jpg"));
The resulting qrcode.jpg:
- Carrying a Logo Icon
QrCodeUtil.generate(//
"http://hutool.cn/", //QR code content
QrConfig.create().setImg("e:/logo_small.jpg"), //Carrying logo
FileUtil.file("e:/qrcodeWithLogo.jpg")//Write out to file
);
The effect is as follows:
- Adjusting Error Correction Level
Many times, QR codes cannot be recognized, and this is when you need to adjust the error correction level. The error correction level is encapsulated using zxing’s ErrorCorrectionLevel
enumeration, including L, M, Q, and H parameters, from low to high. Low-level pixel blocks are larger and can be recognized from a distance, but occlusion can cause failure to recognize. High-level pixel blocks are smaller and allow a certain range of occlusion, but the pixel blocks are denser.
QrConfig config = new QrConfig();
// High error correction level
config.setErrorCorrection(ErrorCorrectionLevel.H);
QrCodeUtil.generate("https://hutool.cn/", config, FileUtil.file("e:/qrcodeCustom.jpg"));
The effect is as follows:
Recognizing QR Code
String decode = QrCodeUtil.decode(FileUtil.file("d:/qrcode.jpg"));
Generating SVG Vector QR Code (since 5.8.6)
QrConfig qrConfig = QrConfig.create().setImg("d:/test/logo.png")
.setForeColor(Color.blue)
.setBackColor(Color.pink)
.setRatio(8)
.setErrorCorrection(ErrorCorrectionLevel.M)
.setMargin(1);
String svg = QrCodeUtil.generateAsSvg("https://hutool.cn/", qrConfig);
Effect:
Generating Ascii Art QR Code (since 5.8.6)
QrConfig qrConfig = QrConfig.create()
.setForeColor(Color.BLUE)
.setBackColor(Color.MAGENTA)
.setWidth(0)
.setHeight(0).setMargin(1);
String asciiArt = QrCodeUtil.generateAsAsciiArt("https://hutool.cn/",qrConfig);
Generated Ascii Art QR Code Text:
[38;5;21;48;5;201mโโโโโโโโโโโโโโโโโโโโโโโโโโโ[0;39m
[38;5;21;48;5;201mโ โโโโโ โโโโโ โโ โโ โโโโโ โ[0;39m
[38;5;21;48;5;201mโ โ โ โโโโโโโโโ โ โ โ โ[0;39m
[38;5;21;48;5;201mโ โโโโโ โโโ โ โโโโโ โโโโโ โ[0;39m
[38;5;21;48;5;201mโโโโโโโโโโโโโโโ โโโโโโโโโโโ[0;39m
[38;5;21;48;5;201mโโโโ โโ โโโโ โโโโโโโโโ โ โ[0;39m
[38;5;21;48;5;201mโ โโโ โโโโโโ โโโโโ โโโโโโโ[0;39m
[38;5;21;48;5;201mโโโ โโโโโโ โโโโโโโโโ โโโโโโ[0;39m
[38;5;21;48;5;201mโ โโโโโโโโ โโโ โโโ โ โ โ[0;39m
[38;5;21;48;5;201mโโโโโโโโโโโ โโ โ โโโ โ โโโ[0;39m
[38;5;21;48;5;201mโ โโโโโ โโโโโโโโโ โโโ โโโโ[0;39m
[38;5;21;48;5;201mโ โ โ โโโโโโโโโโโโโโโโโโโ[0;39m
[38;5;21;48;5;201mโ โโโโโ โโโโ โโโโ โโโโ โ[0;39m
[38;5;21;48;5;201mโโโโโโโโโโโโโโโโโโโโโโโโโโโ[0;39m
Effect of printing to the console: