说明

Java 中通过第三方 jar 实现生成二维码图片根据二维码图片解码文本功能

pom.xml

1
2
3
4
5
6
  <!--二维码-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>

工具类 QRCodeUtil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.bjtcrj.scm.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class QRCodeUtil {
/**
* 生成二维码
*
* @param text 内容,可以是链接或者文本
* @param path 生成的二维码位置
*/
public static String encodeQRCode(String text, String path) {
return encodeQRCode(text, path, null, null, null);
}

/**
* 生成二维码
*
* @param text 内容,可以是链接或者文本
* @param path 生成的二维码位置
* @param width 宽度,默认300
* @param height 高度,默认300
* @param format 生成的二维码格式,默认png
*/
public static String encodeQRCode(String text, String path, Integer width, Integer height, String format) throws RuntimeException {
try {
// 宽
if (width == null) {
width = 300;
}
// 高
if (height == null) {
height = 300;
}
// 图片格式
if (format == null) {
format = "png";
}

// 得到文件对象
String filepath = path + "/" + String.valueOf(UniqueIDGen.getUniqueID()) + "." + format;
File file = new File(Constants.PRO_REAL_PATH + filepath);
// 判断目标文件所在的目录是否存在
if (!file.getParentFile().exists()) {
// 如果目标文件所在的目录不存在,则创建父目录
log.info("目标文件所在目录不存在,准备创建它!");
if (!file.getParentFile().mkdirs()) {
log.info("创建目标文件所在目录失败!");
throw new RuntimeException();
}
}

// 设置字符集编码
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 生成二维码矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
// 二维码路径
Path outputPath = Paths.get(Constants.PRO_REAL_PATH + filepath);
// 写入文件
MatrixToImageWriter.writeToPath(bitMatrix, format, outputPath);
return filepath;
} catch (Exception e) {
log.error(e.getMessage(), e);
e.printStackTrace();
throw new RuntimeException();
}
}

public static void updateQRCode(String text, String path) throws RuntimeException {
try {
// 得到文件对象
File file = new File(Constants.PRO_REAL_PATH + path);
if(file.exists()) {
file.delete();
}
// 判断目标文件所在的目录是否存在
if (!file.getParentFile().exists()) {
// 如果目标文件所在的目录不存在,则创建父目录
log.info("目标文件所在目录不存在,准备创建它!");
if (!file.getParentFile().mkdirs()) {
log.info("创建目标文件所在目录失败!");
throw new RuntimeException();
}
}

// 设置字符集编码
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 生成二维码矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, 300, 300, hints);
// 二维码路径
Path outputPath = Paths.get(Constants.PRO_REAL_PATH + path);
// 写入文件
MatrixToImageWriter.writeToPath(bitMatrix, "png", outputPath);
} catch (Exception e) {
log.error(e.getMessage(), e);
e.printStackTrace();
throw new RuntimeException();
}
}

/**
* 对二维码图片进行解码
*
* @param filePath 二维码路径
* @return 解码后对内容
*/
public static JSONObject decodeQRCode(String filePath) {
try {
// 读取图片
BufferedImage image = ImageIO.read(new File(filePath));

// 多步解析
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);

// 一步到位
// BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)))

// 设置字符集编码
Map<DecodeHintType, String> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");

// 对图像进行解码
Result result = new MultiFormatReader().decode(binaryBitmap, hints);
// 解码内容
JSONObject content = JSONObject.parseObject(result.getText());

System.out.println("图片内容: ");
System.out.println("content: " + content.toJSONString());
System.out.println("图片中格式: ");
System.out.println("encode: " + result.getBarcodeFormat());

return content;
} catch (Exception e) {
log.error(e.getMessage(), e);
}

return null;
}
}