wlzboy
2025-09-27 c1147646b9ef1d713a202d7ab8cf3ea8d677f142
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
package com.ruoyi.common.utils;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 二维码生成工具类
 * 
 * @author ruoyi
 */
public class QRCodeUtils {
    
    private static final Logger log = LoggerFactory.getLogger(QRCodeUtils.class);
    
    private static final int WIDTH = 300;
    private static final int HEIGHT = 300;
    private static final String FORMAT = "png";
    
    /**
     * 生成二维码到文件
     * 
     * @param content 二维码内容
     * @param filePath 文件路径
     * @return 是否生成成功
     */
    public static boolean generateQRCodeToFile(String content, String filePath) {
        try {
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            hints.put(EncodeHintType.MARGIN, 1);
            
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
            
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            
            MatrixToImageWriter.writeToFile(bitMatrix, FORMAT, file);
            return true;
        } catch (Exception e) {
            log.error("生成二维码失败: {}", e.getMessage());
            return false;
        }
    }
    
    /**
     * 生成二维码到字节数组
     * 
     * @param content 二维码内容
     * @return 字节数组
     */
    public static byte[] generateQRCodeToBytes(String content) {
        try {
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            hints.put(EncodeHintType.MARGIN, 1);
            
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
            
            BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, FORMAT, baos);
            
            return baos.toByteArray();
        } catch (Exception e) {
            log.error("生成二维码失败: {}", e.getMessage());
            return null;
        }
    }
    
    /**
     * 生成Base64编码的二维码
     * 
     * @param content 二维码内容
     * @return Base64编码的二维码
     */
    public static String generateQRCodeToBase64(String content) {
        byte[] bytes = generateQRCodeToBytes(content);
        if (bytes != null) {
            return "data:image/png;base64," + java.util.Base64.getEncoder().encodeToString(bytes);
        }
        return null;
    }
 
    /**
     * 生成带车牌号的Base64编码二维码
     * 
     * @param content 二维码内容
     * @param vehicleNo 车牌号
     * @return Base64编码的二维码
     */
    public static String generateQRCodeWithVehicleNoToBase64(String content, String vehicleNo) {
        try {
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            hints.put(EncodeHintType.MARGIN, 1);
            
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
            BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
            
            // 创建带文字的图片
            int textHeight = 40; // 文字区域高度
            int totalHeight = HEIGHT + textHeight;
            BufferedImage finalImage = new BufferedImage(WIDTH, totalHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = finalImage.createGraphics();
            
            // 设置背景为白色
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, WIDTH, totalHeight);
            
            // 绘制二维码
            g2d.drawImage(qrImage, 0, 0, null);
            
            // 绘制车牌号文字
            g2d.setColor(Color.BLACK);
            g2d.setFont(new Font("微软雅黑", Font.BOLD, 16));
            FontMetrics fm = g2d.getFontMetrics();
            int textWidth = fm.stringWidth(vehicleNo);
            int x = (WIDTH - textWidth) / 2;
            int y = HEIGHT + (textHeight + fm.getAscent()) / 2;
            g2d.drawString(vehicleNo, x, y);
            
            g2d.dispose();
            
            // 转换为Base64
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(finalImage, FORMAT, baos);
            return "data:image/png;base64," + java.util.Base64.getEncoder().encodeToString(baos.toByteArray());
            
        } catch (Exception e) {
            log.error("生成带车牌号的二维码失败: {}", e.getMessage());
            return null;
        }
    }
}