0%

【工作技能】二维码生成

关于二维码的生成与使用


1 com.google.zxing依赖

1.1 依赖

1
2
3
4
5
6
7
8
9
10
11
12
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.1</version>
</dependency>

1.2 创建工具类

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
/**
* 二维码工具类
* @author letere
* @create 2021-05-18 11:30
*/
public class QRCodeUtil {

private final static String CODE = "utf-8"; //编码格式
private final static int WIDTH = 300; //二维码宽
private final static int HEIGHT = 300; //二维码高
private final static String FORMAT = "png"; //二维码图片格式


/**
* 生成二维码
* @param outputStream 输出流
* @param data 数据
* @param <T> 泛型
* @throws Exception
*/
public static<T> void create(OutputStream outputStream, T data) throws Exception{
//获取二维码生成参数
Map<EncodeHintType, Object> hints = getEncodeHints();

//将类转成String类型
String content = String.valueOf(data);

//生成二维码信息(编码)
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
BitMatrix encode = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);

//输出二维码
MatrixToImageWriter.writeToStream(encode, FORMAT, outputStream);
}


/**
* 获取二维码编码参数
* @return Map<EncodeHintType, Object>
*/
private static Map<EncodeHintType, Object> getEncodeHints() {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, CODE); //编码格式
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); //纠错等级
hints.put(EncodeHintType.MARGIN, 1); //外边距(白边)

return hints;
}


/**
* 读取二维码
* @param inputStream 输入流
* @param c 返回数据类型
* @param <T> 泛型
* @return T
* @throws Exception
*/
public static<T> T read(InputStream inputStream, Class<T> c) throws Exception{
//读取二维码图片
BufferedImage image = ImageIO.read(inputStream);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));

//读取二维码中的内容(解码)
Map<DecodeHintType, Object> hints = getDecodeHints();
MultiFormatReader formatReader = new MultiFormatReader();
Result result = formatReader.decode(binaryBitmap, hints);

//类型强转为指定指定数据类型
return (T) result.getText();
}


/**
* 获取二维码解码参数
* @return Map<DecodeHintType, Object>
*/
private static Map<DecodeHintType, Object> getDecodeHints() {
Map<DecodeHintType, Object> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, CODE); //解码格式

return hints;
}
}

1.3 代码测试

  • (1)生成二维码测试
    1
    2
    3
    4
    5
    6
    7
    8
    9
    @Test
    public void fileTest() throws Exception{
    String text = "https://www.bilibili.com";
    File file = new File("./src/main/resources/qr.png");
    FileOutputStream fos = new FileOutputStream(file);

    //工具类生成二维码(输出流,二维码内容)
    QRCodeUtil.create(fos, text);
    }
  • (2)读取二维码测试
    1
    2
    3
    4
    5
    6
    7
    8
    9
    @Test
    public void fileTest() throws Exception{
    File file = new File("./src/main/resources/qr.png");
    FileInputStream fis = new FileInputStream(file);

    //工具类读取二维码图片内容(图片输入流,读取后的数据类型)
    String data = QRCodeUtil.read(fis, String.class);
    System.out.println(data);
    }