java生成二维码技术实现
一.第一种
maven依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>
java 代码
/**
* 生成二维码 生成一个新的页面
* @param request
* @param response
*/
@ApiOperation(value = "生成二维码,生成一个新的页面")
@RequestMapping("/createQRcode")
public void createQRcode(HttpServletRequest request, HttpServletResponse response) {
String contents = "https://www.baidu.com/";
int width = 500; int height = 500; int margin = 2;
try {
BufferedImage QRcode = QrCodeCreateUtil.createQRImage(contents, width, height, margin);
String logoPath = request.getSession().getServletContext().getRealPath("") + "QR" + File.separator;
// String logoPath = "C:\\Users\\zy962\\Desktop\\临时备份\\u=1792043407,969529386&fm=26&gp=0.jpg";
File file2 = new File(logoPath);
if(!file2.exists() && !file2.isDirectory()){
file2.mkdirs();
}
logoPath = logoPath + System.currentTimeMillis()+"_"+new Random().nextInt(1000) + ".jpg";
file2 = new File(logoPath);
if (!file2.exists()) {
file2.createNewFile();
}
System.out.println("生成图片:" + logoPath);
int logoSize = 4;
BufferedImage qRImageWithLogo = QrCodeCreateUtil.addQRImagelogo(QRcode, width, height, logoPath, logoSize);
// 写入返回
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(qRImageWithLogo, "jpg", baos);
byte[] QRJPG = baos.toByteArray();
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream os = response.getOutputStream();
os.write(QRJPG); // 自此完成一套,图片读入,写入流,转为字节数组,写入输出流
os.flush();
os.close();
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
第二种
maven依赖
<dependency>
<groupId>net.glxn.qrgen</groupId>
<artifactId>javase</artifactId>
<version>2.0</version>
</dependency>
java 代码
/**
* 生成二维码 在列表中显示
* @param request
* @param response
* @throws IOException
*/
@RequestMapping("/getPng")
@ApiOperation(value = "生成二维码 在列表中显示")
public void getPng(HttpServletRequest request,HttpServletResponse response) throws IOException {
String url = "https://www.baidu.com/";
ByteArrayOutputStream out = QRCode.from(url).to(ImageType.PNG).stream();
response.setContentType("image/png");
response.setContentLength(out.size());
OutputStream os = response.getOutputStream();
os.write(out.toByteArray());
os.flush();
os.close();
}
// 生成二维码图片
public void code() {
try {
String content = "https://www.baidu.com";
String path = "D:\\upload\\img\\livecode";// 二维码保存的路径
String codeName = UUID.randomUUID().toString();// 二维码的图片名
String imageType = "jpg";// 图片类型
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400, hints);
File file1 = new File(path, codeName + "." + imageType);
MatrixToImageWriter.writeToFile(bitMatrix, imageType, file1);
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
使用img标签调用getPng方法