springboot生成二维码

1:添加依赖

    <!-- 二维码支持包 -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.2.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.2.0</version>
        </dependency>

 

  controller层:

1:获取要生成二维码的内容传给前端

 //输入二维码要生成的内容
    @RequestMapping(value = "/open")
    public ModelAndView open( String qrid1){
        ModelAndView mv=new ModelAndView();
        mv.addObject("qrid",qrid1);
        mv.setViewName("/qrcode.btl");
        return mv;
    }

前端页面:

 <form id="enter" action="/qr/open" method="post" class="form-horizontal form-label-left">
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12">票编号</label>
                                        <div class="col-md-9 col-sm-9 col-xs-12">
                                            <input name="qrid1" id="qrid1" type="text" class="form-control" placeholder="请输入有效的票编号"required>
                                            <span style="color: red" id="warning"></span>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="submit"   class="btn btn-primary">提交</button>
                                        </div>
                                    </div>
                                </form>

这里再通过调用输入的东西生成二维码图片:

   <img src="/qr/qrcode?content=${qrid}"/>

上面src调用了/qr/qrcode

把输入的qrid传入进去。

注意action的地址,我controller的地址是/qr 所以我地址前面都写了/qr

 /**
     * 生成二维码方法
     *
     * @param  content 要生成的内容
     * @param resp response对象
     * @throws Exception 抛出异常
     */
    @RequestMapping(value = "/qrcode")
    public void getQrcode(String content, HttpServletResponse resp) throws Exception {
        ServletOutputStream stream = null;
       /* Long qrid=Long.parseLong(content);
        Ticket ticket=ticketRepository.findByTicketId(qrid);
       if (ticket!=null) {*/
           try {
               stream = resp.getOutputStream();
               Map<EncodeHintType, Object> hints = new HashMap<>();
               //编码
               hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
               //边框距
               hints.put(EncodeHintType.MARGIN, 0);
               QRCodeWriter qrCodeWriter = new QRCodeWriter();
               BitMatrix bm = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 200, 200, hints);
               MatrixToImageWriter.writeToStream(bm, "png", stream);
           } catch (WriterException e) {
               e.getStackTrace();

           } finally {
               if (stream != null) {
                   stream.flush();
                   stream.close();
               }
           }
       }

一开始刚打开这个页面是没有填写数据的,应该写一个方法打开这个页面,赋好值

//打开生成二维码页面。
    @RequestMapping(value = "/open1")
    public ModelAndView open1(String qrid1){
        ModelAndView mv = new ModelAndView();
        qrid1="当前票为空"; 
        mv.addObject("qrid",qrid1);
        mv.setViewName("/qrcode.btl");
        return mv;
    }

然后就在可以在前端页面输入你要生成二维码的内容啦。

刚打开页面:

 

 输入数据后:

 

posted @ 2018-08-19 09:18  Hiro-D  阅读(10349)  评论(0编辑  收藏  举报