【代码】Java生成二维码

前言

Java生成二维码

源代码

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
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Base64Utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;

/**
* 二维码工具类
*/
//@Component
public class QrCodeUtils {

// 制定二维码宽度
private static int weight = 400;

// 制定二维码高度
private static int height = 400;

// 制定转换的格式
private static String format = "png";

/**
* 生成二维码并base64转码(默认参数)
*
* @param content 二维码内容
* @return 二维码图片经base64转码后的结果
*/
public static String encodeWithBase64(String content) {
return "data:image/" + format + ";base64," + encodeWithBase64(content, weight, height, format);
}

/**
* 生成二维码并base64转码
*/
public static String encodeWithBase64(String content, int width, int height, String format) {
BufferedImage bufferedImage = encodeOfImg(content, width, height, format);

try {
// 转为字节数组
byte[] imageByteArray = IOUtils.toByteArray(bufferedImageToInputStream(bufferedImage, format));

// base64转码
return Base64Utils.encodeToString(imageByteArray);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 生成二维码并保存文件
*/
public static void encodeAndSave(String content, int width, int height, String format, String path) {
// 生成二维码位图
BitMatrix bitMatrix = encode(content, width, height, format);

try {
MatrixToImageWriter.writeToPath(bitMatrix, format, new File(path).toPath());
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 生成二维码位图
*/
private static BitMatrix encode(String content, int width, int height, String format) {
HashMap hints = new HashMap();
//编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//容错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
//边框
hints.put(EncodeHintType.MARGIN, 2);
try {

BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);

return bitMatrix;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 生成二维码图片对象
*/
private static BufferedImage encodeOfImg(String content, int width, int height, String format) {
try {
// 生成二维码位图
BitMatrix bitMatrix = encode(content, width, height, format);

// 转为图片
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix,
new MatrixToImageConfig(Color.BLACK.getRGB(), Color.WHITE.getRGB()));

return bufferedImage;
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

private static InputStream bufferedImageToInputStream(BufferedImage image, String format) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(image, format, os);
InputStream input = new ByteArrayInputStream(os.toByteArray());
return input;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

完成