Java后台生成二维码方法

引入依赖:

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.2.5</version>
</dependency>

<!--二维码工具类-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>

 

生成代码:

public static void main(String[] args) {
    QrConfig qrConfig = new QrConfig(300, 300);
    qrConfig.setMargin(1);
byte[] bytes = QrCodeUtil.generatePng("www.baidu.com", qrConfig);
// ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();

FileOutputStream fos = new FileOutputStream("C:\\Users\\v\\Desktop\\temp\\二维码.png");

bos.write(bytes);
bos.writeTo(fos);
fos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {

}

}

posted @ 2021-06-29 16:36  易不易  阅读(387)  评论(0编辑  收藏  举报