Java生产二维码

Java生产二维码

1.添加依赖

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
        </dependency>

二维码生产工具类

/**
 * 生成发送二维码方法
 *
 * @param text     二维码生成规则(二维码可以是任何英文字母加数字生成的二维码)
 * @param width    宽度
 * @param height   高度
 * @param filePath 输出图片地址
 */
    private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
        //生成二维码类
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        //生成的二维码
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        //生产UUID
        String s = UUID.randomUUID().toString();
        //去掉“-”符号
        String s1 = s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
        //生产UUID当做生产照片的名字
        String uuid=s1+".png";
        //路径+照片名称
        String filePaths = filePath + uuid;
        //二维码输出地址
        Path path = FileSystems.getDefault().getPath(filePaths);
        //输出二维码图片
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);

    }

调用二维码工具类生产二维码到D盘

 public static void main(String[] args) {
        try {
            //二维码内的信息
            String info = "https://www.baidu.com";
            String path="D:\";
            //调用二维码工具类接口
            generateQRCodeImage(info, 350, 350,path);
        } catch (WriterException e) {
            System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值