1.二维码介绍
二维码QR Code(Quick Response Code)
由Denso公司于1994年9月研制的一种矩阵二维码符号,它具有一维条码及其它二维条码所具有的信息容量大、可靠性高、可表示汉字及图象多种文字信息、保密防伪性强等优点。
ZXing
一个支持在图像中解码和生成条形码(如二维码、PDF 417、EAN、UPC、Aztec、Data Matrix、Codabar)的库。ZXing(“zebra crossing”)是一个开源的、多格式的、用Java实现的一维/二维条码图像处理库,具有到其他语言的端口。
2.代码功能
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 backgroundconfig.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 streamQrCodeUtil.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/", //contentQrConfig.create().setImg("D:\\tmp\\logo.png"), //logoFileUtil.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);}
}
代码仓库
https://github.com/Harries/springboot-demo
3.测试
原始zxing生成qrcode
访问http://127.0.0.1:8088/original/qrcode/image?orderNo=222,查看目录下生成的文件
采用hutool生成qrcode
访问http://127.0.0.1:8088//hblog/qrcode/image2,查看文件
生成带有logo的二维码
访问http://127.0.0.1:8088//hblog/qrcode/image1,查看文件
4.引用
https://www.bookstack.cn/read/hutool/18a69dd68fd334c8.md
http://www.liuhaihua.cn/archives/710338.html
https://zxing.github.io/zxing/