• Java生成二维码


    创建二维码

    依赖导入

        <dependency>
                
                <groupId>com.google.zxinggroupId>
                <artifactId>coreartifactId>
                <version>3.4.1version>
            dependency>
            
            <dependency>
                <groupId>com.google.zxinggroupId>
                <artifactId>javaseartifactId>
                <version>3.4.1version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    工具类

    package com.sin.util;
    
    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.qrcode.decoder.ErrorCorrectionLevel;
    
    import javax.imageio.ImageIO;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @createTime 2023/10/17 10:07
     * @createAuthor SIN
     * @use 二维码创建工具类
     */
    public class QRCodeUtil {
    
    
        private static final int WIDTH = 300; // 二维码宽度
        private static final int HEIGHT = 300; // 二维码高度
        private static final String FORMAT = "png"; // 二维码格式
    
        /**
         * 生成二维码
         * @param text 二维码内容,一定是互联网内容,本地是无法扫的到的
         * @param filePath 二维码生成地址
         */
        public static void generateQRCode(String text, String filePath) {
            try {
                
                // 创建一个存储编码参数的 HashMap 对象
                Map<EncodeHintType, Object> hints = new HashMap<>();
                // 设置字符集为 UTF-8
                hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                // 设置容错级别为 M
                hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
                // 设置边距为 2
                hints.put(EncodeHintType.MARGIN, 2);
    
                // 使用 MultiFormatWriter 类将指定文本 text 编码为二维码矩阵
                BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
    
                // 将二维码矩阵转换为 BufferedImage 对象
                BufferedImage image = toBufferedImage(bitMatrix);
    
                // 创建一个表示目标文件的 File 对象
                File file = new File(filePath);
                // 将 BufferedImage 对象保存为图像文件
                ImageIO.write(image, FORMAT, file);
    
                System.out.println("二维码已生成,保存路径:" + filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 将二维码矩阵转为可视化图像
         * @param matrix
         * @return 生成BufferdImage对象,获得二维码的可视化图像
         */
        private static BufferedImage toBufferedImage(BitMatrix matrix) {
            // 获取二维码的宽和高
            int width = matrix.getWidth();
            int height = matrix.getHeight();
            // 创建一个新的BufferedImage对象,指定宽和高,已经颜色类型
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    // 获取当前坐标 (x, y) 的像素值(true 表示黑色,false 表示白色)
                    boolean pixel = matrix.get(x, y);
                    // 根据像素值设置对应位置的 RGB 值,将黑色或白色像素点绘制到 BufferedImage 对象中
                    int rgb = pixel ? Color.BLACK.getRGB() : Color.WHITE.getRGB();
                    // 设置x,y坐标和颜色
                    image.setRGB(x, y, rgb);
                }
            }
            // 返回生成的 BufferedImage 对象
            return image;
        }
    }
    
    
    • 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

    测试

    package com.sin.test;
    
    import com.sin.util.QRCodeUtil;
    
    public class QRCodeExample {
    
    
        public static void main(String[] args) {
            String text = "https://blog.csdn.net/qq_44715376?spm=1000.2115.3001.5343"; // 二维码中的文本内容
            String filePath = "D:/qrcode.png"; // 二维码保存路径
    
            QRCodeUtil.generateQRCode(text, filePath);
            System.out.println("生成成功");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    生成的二维码

    在这里插入图片描述

  • 相关阅读:
    面试机器学习你一定会遇到的知识点汇总
    记录第一次开源流计算框架Flink代码的贡献
    配置oem 13c以监控数据库
    使用 Prometheus 实现邮件/企业微信告警
    守护线程和非守护线程
    计算机专业毕业设计项目推荐08-英语在线点读平台(SpringBoot+Vue+MongoDB)
    《剑指 Offer 》—16. 数值的整数次方
    DS18B20数字温度计 (一) 电气特性, 寄生供电模式和远距离接线
    法律战爆发:“币安退出俄罗斯引发冲击波“
    MYSQL02高级_目录结构、默认数据库、表文件、系统独立表空间
  • 原文地址:https://blog.csdn.net/qq_44715376/article/details/133918834