利用谷歌的zxing包来实现二维码的生成。
1、添加maven依赖
<!--二维码-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.1.0</version>
</dependency>
2、Java代码块
public void getCode1(String phone)throws WriterException, IOException {
//生成的二维码的路径
// String QR_CODE_IMAGE_PATH = "D:\\IdeaProjects\\ruiyi-ms\\target\\classes\\META-INF\\resources\\code.png";
String QR_CODE_IMAGE_PATH = "D:\\ruoyi\\uploadPath\\avatar\\code.png";
//二维码中的内容
String QR_CODE_TEXT = phone;
//二维码图片的宽度
int WIDTH = 300;
//二维码图片的高度
int HEIGHT = 300;
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//H最高容错等级
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = qrCodeWriter.encode(QR_CODE_TEXT, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
Path path = FileSystems.getDefault().getPath(QR_CODE_IMAGE_PATH);
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}
这种方式是生成二维码 还有生成流返回给前台
https://blog.csdn.net/weixin_47316183/article/details/125275108https://blog.csdn.net/qq_21234103/article/details/120620468?utm_term=java%E7%94%9F%E6%88%90%E4%BA%8C%E7%BB%B4%E7%A0%81%E6%A0%87%E7%AD%BE&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-0-120620468-null-null&spm=3001.4430