Spring Boot integrates zxing to implement the function of generating QR codes

HBLOG
3 min readMar 23, 2024

--

1.Introduction to QR code

Quick Response Code

A matrix two-dimensional code symbol developed by Denso Company in September 1994. It has the large information capacity and high reliability of one-dimensional bar codes and other two-dimensional bar codes. It can represent a variety of text information such as Chinese characters and images. Confidentiality and strong anti-counterfeiting properties.

ZXing

A library that supports decoding and generating barcodes (such as QR codes, PDF 417, EAN, UPC, Aztec, Data Matrix, Codabar) in images. ZXing (“zebra crossing”) is an open source, multi-format, 1D/2D barcode image processing library implemented in Java, with ports to other languages.

2.Code project

Experiment purpose: Use zxing or hutool to generate QR code

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>qrcode</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.15</version>
</dependency>
</dependencies>
</project>

applicatio.yaml

server:
port: 8088

QRCodeGenerator.java

package com.et.qrcode.util;
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.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Base64;
/**
* QRCodeGenerator
*
* @author zhouzhaodong
*/
public class QRCodeGenerator {
/**
* generateQRCodeImage
* @param text
* @param width
* @param height
* @param filePath
* @throws WriterException
* @throws IOException
*/
public static void generateQRCodeImage(String text, int width, int height, String filePath)
throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
Path path = FileSystems.getDefault().getPath(filePath);
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}
/**
* writeToStream
* @param text
* @param width
* @param height
* @return
*/
public static String writeToStream(String text, int width, int height) {
String message = "";
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix;
try {
bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream);
Base64.Encoder encoder = Base64.getEncoder();
message = encoder.encodeToString(outputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
}

Controller

package com.et.qrcode.controller;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.et.qrcode.util.QRCodeGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.awt.*;
/**
* generate qrcode
* @author zhouzhaodong
*/
@Controller
public class QRCodeController {
/**
* qrcodeImage
* @param orderNo
* @return
*/
@RequestMapping("/original/qrcode/image")
public String qrcodeImage(String orderNo) {
String failPath = "D:\\tmp\\" + orderNo + ".png";
try {
QRCodeGenerator.generateQRCodeImage(orderNo, 350, 350, failPath);
} catch (Exception e) {
e.printStackTrace();
}
return failPath;
}
/**
* qrcodeBase64
* @param orderNo
* @return
*/
@RequestMapping("/original/qrcode/base64")
public String qrcodeBase64(String orderNo) {
String message = "";
try {
message = QRCodeGenerator.writeToStream(orderNo, 350, 350);
} catch (Exception e) {
e.printStackTrace();
}
return message;
}

@RequestMapping("/hblog/qrcode/image1")
public String hutoolsImages1() {
String message = "";
try {
QrConfig config = new QrConfig(300, 300);
// Set the margin, that is, the margin between the QR code and the background
config.setMargin(3);
// Set the foreground color, which is the QR code color (cyan)
config.setForeColor(Color.CYAN.getRGB());
// Set background color (gray)
config.setBackColor(Color.GRAY.getRGB());
// Generate QR code to file or stream
QrCodeUtil.generate("http://www.liuhiahua.cn/", config, FileUtil.file("D:\\tmp\\hblog1.png"));
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
@RequestMapping("/hblog/qrcode/image2")
public String hutoolsImages2() {
String message = "";
try {
QrCodeUtil.generate(//
"http://www.liuhiahua.cn/", //content
QrConfig.create().setImg("D:\\tmp\\logo.png"), //logo
FileUtil.file("D:\\tmp\\qrcodeWithLogo.jpg")//output file
);
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
}

DemoApplication.java

package com.et.qrcode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

Code Repository

3.Test

Original zxing generates qrcode

Visit http://127.0.0.1:8088/original/qrcode/image?orderNo=222 to view the files generated in the directory

Use hutool to generate qrcode

Visit http://127.0.0.1:8088//hblog/qrcode/image2 to view the file

Generate QR code with logo

Visit http://127.0.0.1:8088//hblog/qrcode/image1 to view the file

4.reference

--

--

HBLOG
HBLOG

Written by HBLOG

talk is cheap ,show me your code

No responses yet