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>
2,生成二维码工具类
package cn.hyt.hyt_zhdj_cs.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
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;
public class QRCodeGenerator {
//生成为字节数组
public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
byte[] pngData = pngOutputStream.toByteArray();
return pngData;
}
//生成二维码图片,并保存在桌面
public 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);
Path path = FileSystems.getDefault().getPath(filePath);
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}
}
3,调用:
3.1: 二维码图片,返回到前端
@ApiOperation(value = "二维码")
@GetMapping("qrcode")
public AppResponseJson qrcode(HttpServletResponse response) throws Exception {
// 存放在二维码中的内容
String text = "我是小铭";
// 嵌入二维码的图片路径
String imgPath = null;
// 生成的二维码的路径及名称
String destPath = "E:/untitled/jam.jpg";
//生成二维码,true:表示将嵌入二维码的图片进行压缩,如果为“false”则表示不压缩。
QRCodeUtil.encode(text, imgPath, destPath, true);
File file = new File(destPath);
if(file.exists()) {
FileInputStream in = new FileInputStream(file);
OutputStream os = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename="+destPath);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.setContentType("application/msexcel");
byte[] b = new byte[1024];
while(in.read(b)!= -1) {
os.write(b);
}
in.close();
os.flush();
os.close();
}
return AppResponseJson.successRequest("成功");
}
3.2:二维码byte数组,返回到前端
@ApiOperation(value = "二维码1")
@GetMapping("qrcode1")
public AppResponseJson qrcode1(HttpServletResponse response) throws WriterException, IOException {
//二维码内的信息
String info = "This is my first QR Code";
byte[] qrcode = QRCodeGenerator.getQRCodeImage(info, 360, 360);
OutputStream os = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=zsc.jpg");
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.setContentType("application/msexcel");
os.write(qrcode);
os.flush();
os.close();
return AppResponseJson.successRequest("成功");
}