zoukankan      html  css  js  c++  java
  • Zxing二维码生成

    http://www.manongjc.com/article/18411.html

    首先,在pom.xml文件中添加依赖

    <dependency>
       <groupId>com.google.zxing</groupId>
       <artifactId>core</artifactId>
       <version>3.3.3</version>
       <scope>compile</scope>
    </dependency>
    <dependency>
       <groupId>com.google.zxing</groupId>
       <artifactId>javase</artifactId>
       <version>3.3.3</version>
       <scope>compile</scope>
    </dependency>
    其次,Controller的写法(生成二维码并以流的形式输出到浏览器)
    
    @RequestMapping("/test")
    public void dowanload(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //二维码中包含的信息
        String content = "姓名:十二余
    博客:https://www.cnblogs.com/jing5464";
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        // 指定编码格式
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 指定纠错级别(L--7%,M--15%,Q--25%,H--30%)
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 编码内容,编码类型(这里指定为二维码),生成图片宽度,生成图片高度,设置参数
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200, hints);
        //设置请求头
        response.setHeader("Content-Type","application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + "十二余的二维码.png");
        OutputStream outputStream = response.getOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);
        outputStream.flush();
        outputStream.close();
    }

    页面展示标签:

    <img src="http://localhost:20010/projectname/zxing/test" style="80;height:80"/>

  • 相关阅读:
    《代码阅读方法与实践》阅读笔记之二
    《代码阅读方法与实践》阅读笔记一
    专业实训题目需求分析
    阅读计划
    第二阶段Sprint10
    第二阶段Sprint9
    第二阶段Sprint8
    第二阶段Sprint7
    第二阶段个人工作总结(8)
    第二阶段个人工作总结(7)
  • 原文地址:https://www.cnblogs.com/keyi/p/13700897.html
Copyright © 2011-2022 走看看