编程知识 cdmana.com

Java:com.google.zxing:javase生成二维码

依赖

<!--生成二维码-->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.0</version>
</dependency>

代码示例

package com.demo.qrcode;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;


public class QRCodeGenerator {
    


    public static void main(String[] args) throws WriterException, IOException {
    
        // 设置参数
        String text = "Hello World";
        int width = 350;
        int height = 350;

        Path path = FileSystems.getDefault().getPath("demo.png");

        // 生成二维码
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);

        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);


    }
}

在这里插入图片描述

参考
几行代码,轻松带你实现Java生成二维码功能!

版权声明
本文为[彭世瑜]所创,转载请带上原文链接,感谢
https://pengshiyu.blog.csdn.net/article/details/111661603

Scroll to Top