zjh
2024-04-26 d183f6f3f4dd871721e9bd3f30a4e8c71b4a9f1b
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
package com.ltkj.web.controller.barcode;
 
/**
 * @Company: 西安路泰科技有限公司
 * @Author: lige
 * @Date: 2023/2/16 11:06
 */
import java.awt.image.BufferedImage;
import com.google.zxing.common.BitMatrix;
 
 
/**
 * 二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类直接拷贝到源码中使用
 */
public class MatrixToImageWriter {
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
 
    private MatrixToImageWriter() {
    }
 
    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }
 
}