JAVA实现动态⼆维码输出
⼀、控制层
1 @GetMapping(value = "/generate")
2 @ResponBody
3public void generateQR(@RequestParam("url")String url, HttpServletRespon respon) {
4 BufferedImage imageWithLogo;
5 respon.tHeader("Pragma", "No-cache");
6 respon.tHeader("Cache-Control", "no-cache");
7 respon.tDateHeader("Expires", 0);
8 respon.tContentType("image/jpeg");
9 InputStream resourceAsStream = Class().getResourceAsStream("/logo/logo.png");
10 imageWithLogo = ateImageWithLogo(url, resourceAsStream, true);
11try(ServletOutputStream sos = OutputStream()){
12 ImageIO.write(imageWithLogo, "jpeg", sos);
13 } catch (IOException e) {
14 e.printStackTrace();
15 }
16 }
⼆、QRCodeUtil
1package port.biz.utils;
2
le.zxing.BarcodeFormat;fax传真
le.zxing.EncodeHintType;
le.zxing.MultiFormatWriter;
le.zxing.WriterException;
BitMatrix;
le.zxing.qrcode.decoder.ErrorCorrectionLevel;
9
10import javax.imageio.ImageIO;
11import java.awt.*;
12import RoundRectangle2D;
13import java.awt.image.BufferedImage;
14import java.io.IOException;
15import java.io.InputStream;
16import java.util.Hashtable;
17
18/**
19 * @Auther: ShouZhi@Duan
放松心情的方法20 * @Date: 2021/3/22 15:18
21 * @Description:
22*/
23public class QRCodeUtil {
24private static final String CHARSET = "utf-8";
25
26// ⼆维码尺⼨
27private static final int QRCODE_SIZE = 300;
28// LOGO宽度
29private static final int LOGO_WIDTH = 80;
30// LOGO⾼度
31private static final int LOGO_HEIGHT = 80;
32
33/**
34 * ⽣成不含Logo的⼆维码
35 * @param content ⼆维码跳转资源
36 * @return
37*/
38public static BufferedImage createImageNoLogo(String content) {
39 Hashtable<EncodeHintType, Object> hints = new Hashtable();
40 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
面点培训中心
41 hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
42 hints.put(EncodeHintType.MARGIN, 1);
43 BitMatrix bitMatrix = null;
44try {
45 bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
46 } catch (WriterException e) {
47 e.printStackTrace();
48 }
49int width = Width();
50int height = Height();
51 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
52for (int x = 0; x < width; x++) {
53for (int y = 0; y < height; y++) {
54 image.tRGB(x, y, (x, y) ? 0xFF000000 : 0xFFFFFFFF);
55 }
56 }
57return image;
58 }
59
spinster
60
61/**
62 * ⽣成含Logo的⼆维码
63 * @param content ⼆维码跳转资源
64 * @param logoStream Logo图⽚资源
65 * @param compress Logo是否需要压缩
66 * @return
英孚英语培训费用67*/
68public static BufferedImage createImageWithLogo(String content, InputStream logoStream, boolean compress) {
69 Hashtable<EncodeHintType, Object> hints = new Hashtable();
70 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
71 hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
72 hints.put(EncodeHintType.MARGIN, 1);
73 BitMatrix bitMatrix = null;
74try {
75 bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
76 } catch (WriterException e) {
77 e.printStackTrace();
78 }
79int width = Width();
80int height = Height();
81 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
82for (int x = 0; x < width; x++) {
83for (int y = 0; y < height; y++) {
84 image.tRGB(x, y, (x, y) ? 0xFF000000 : 0xFFFFFFFF);
85 }
86 }
87 inrtImage(image,logoStream,compress);
88return image;
89 }
90
91
92/**
93 * 插⼊LOGO
94 * @param source ⼆维码图⽚
95 * @param logoPath LOGO图⽚地址
96 * @param needCompress 是否压缩
97 * @throws Exception
98*/
99public static void inrtImage(BufferedImage source, InputStream logoPath, boolean needCompress) {
100 Image src = null;
101try {
102 src = ad(logoPath);
103 } catch (IOException e) {
sat出分时间104 e.printStackTrace();
105 }
106int width = Width(null);
十二月份英文107int height = Height(null);
108if (needCompress) {
109// 压缩LOGO
110if (width > LOGO_WIDTH) {
111 width = LOGO_WIDTH;
112 }
113if (height > LOGO_HEIGHT) {
114 height = LOGO_HEIGHT;
115 }
116 Image image = ScaledInstance(width, height, Image.SCALE_SMOOTH);
117 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
118 Graphics g = Graphics();
119// 绘制缩⼩后的图
fif
120 g.drawImage(image, 0, 0, null);
121 g.dispo();教程英文
122 src = image;
123 }
124// 插⼊LOGO
125 Graphics2D graph = ateGraphics();
126int x = (QRCODE_SIZE - width) / 2;
127int y = (QRCODE_SIZE - height) / 2;
128 graph.drawImage(src, x, y, width, height, null);
129 Shape shape = new RoundRectangle2D.Float(x, y, width, width, 12, 12);
ive130 graph.tStroke(new BasicStroke(3f));
131 graph.draw(shape);
132 graph.dispo();
133 }
134 }