wlzboy
2025-09-22 fe95f471666d93e7822a4886c1c69dafbd6b2a1e
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
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.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;
    }
}