郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
package com.gk.hotwork.specialWork.util;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 二维码生成工具
 */
public class QrCodeUtil {
    //编码
    private static final String CHARSET = "UTF-8";
    //文件格式
    private static final String FORMAT_NAME = "JPG";
 
    // 宽度
    private static final int WIDTH = 80;
    // 高度
    private static final int HEIGHT = 80;
 
    public QrCodeUtil() {
    }
 
    public static BitMatrix createQrCode(String text) {
        try {
            Map<EncodeHintType, String> hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
            hints.put(EncodeHintType.MARGIN, "0");//二维码空白区域,最小为0也有白边,只是很小,最小是6像素左右
            BitMatrix bitMatrix = (new MultiFormatWriter()).encode(text, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
            return bitMatrix;
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }
 
    static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, file)) {
            throw new IOException("Could not write an image of format " + format + " to " + file);
        }
    }
 
    static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, stream)) {
            throw new IOException("Could not write an image of format " + format);
        }
    }
 
    private static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, 1);
 
        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                image.setRGB(x, y, matrix.get(x, y) ? -16777216 : -1);
            }
        }
        return image;
    }
 
    /**
     * 生成二维码字节流
     */
    public static ByteArrayOutputStream generate(String text) throws IOException {
        //获取一个二维码图片
        BitMatrix bitMatrix = QrCodeUtil.createQrCode(text);
        //以流的形式输出到前端
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, FORMAT_NAME, outputStream);
        return outputStream;
    }
 
 
    public static void main(String[] args) throws IOException {
        //获取一个二维码图片
        BitMatrix bitMatrix = QrCodeUtil.createQrCode("测试");
 
 
        //以流的形式输出到前端
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream);
 
        //转换为base64
        Base64.Encoder encoder1 = Base64.getEncoder();
        String advUrl = "data:image/jpeg;base64,"
                + encoder1.encodeToString(outputStream.toByteArray());
        //打印base64结果
        System.out.println(advUrl);
 
    }
}