zxing生成二維碼以流式傳到頁面
阿新 • • 發佈:2018-11-28
@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);