引入maven依赖
<!-- google二维码生成包 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.0</version>
</dependency>
String code_url="java程序猴";//定义转成二维码的内容
if(code_url==null){
throw new NullPointerException();
}
//生成二维码配置
Map<EncodeHintType,Object> hints=new HashMap<>();
//设置纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
//编码类型
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
//bitMatrix对象生成二维码
BitMatrix bitMatrix=new MultiFormatWriter().encode(code_url, BarcodeFormat.QR_CODE,400,400,hints);
OutputStream out=response.getOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix,"png",out);
此种方式是我常用的生成二维码方式 感觉很好用 各位大神请笑纳!
推荐二维码学习地址:
https://coolshell.cn/articles/10590.html
原文:https://www.cnblogs.com/java-cxh/p/12910697.html