@RequestMapping(“/makeQrCode”)
public void madeQrCode(HttpServletResponse res,String content,Integer width,Integer height) throws IOException{
String format = "png";
content = (content == null || "".equals(content) ? "http://www.manjiwang.com" : content);
width = (width == null ? 300 : width);
height = (height == null ? 300 : height);
System.out.println(content);
System.out.println(width);
System.out.println(height);
if(content != null && !"".equals(content)){
ServletOutputStream stream = null;
try {
//定义二维码的参数
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN, 2);
//生成二维码
stream = res.getOutputStream();
BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height , hints);
MatrixToImageWriter.writeToStream(bitMatrix, format, stream);
} catch (WriterException e) {
e.printStackTrace();
}finally{
if(stream != null){
stream.flush();
stream.close();
}
}
}
}
前端页面接收
$(“#image”).attr(“src”,”/backstage/makeQrCode?content=”+content+”&width=”+width+”&height=”+height);