zxing生成二维码以流式传到页面

来源:互联网 发布:汕头大学网络教育 编辑:程序博客网 时间:2024/05/24 16:17

@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);

0 0