springMVC 将url生成二维码显示到jsp页面

1.maven的pom.xml引入google的zxing依赖

<!-- 生成二维码工具-->
<dependency>
   <groupId>com.google.zxing</groupId>
   <artifactId>core</artifactId>
   <version>3.3.0</version>
</dependency>
<dependency>
   <groupId>com.google.zxing</groupId>
   <artifactId>javase</artifactId>
   <version>3.3.0</version>
</dependency>

2.分享按钮所在的页面

<button class="btn btn-default" type="button" id="share" title="分享" onclick="share('${id}')">
	<i class="fa fa-share-square-o"></i>分享
</button>

3.分享按钮调用的js方法

 //分享按钮
 function share(id){
  	var adminPath = window.document.location.host;

  	var url = "http://"+adminPath+"/count/compositeSearch/share/"+id;
  	//这里的jp是jeeplus.js
	jp.openViewDialog("分享二维码","${ctx}/count/compositeSearch/qrcode?url="+url,width*0.6+'px', height*0.5+"px");
  }

4.jeeplus.js里jp.openViewDialog方法

//打开对话框(查看)
openViewDialog :function(title,url,width,height){
  var auto = true;//是否使用响应式,使用百分比时,应设置为false
  if(width.indexOf("%")>=0 || height.indexOf("%")>=0 ){
     auto =false;
  }
  top.layer.open({
      type: 2,
      area: [width, height],
      title: title,
      auto:auto,
      maxmin: true, //开启最大化最小化按钮
      content: url ,
      btn: ['关闭'],
      cancel: function(index){
      }
 });
}

5.controller层进入jsp页面的代码和生成二维码的代码

    /**
	 * 生成分享二维码的页面
	 * @return
	 */
	@GetMapping("qrcode")
	public String qrcode(String url,Model model) {
		model.addAttribute("url",url);
		return "/count/compositeSearch/shareQrCode";
	}

	/**
	 * 生成二维码
	 * @param url
	 * @param response
	 */
	@RequestMapping(value = "/getErWeiCode", method = {RequestMethod.POST, RequestMethod.GET})
	public void getErWeiCode(String url,HttpServletResponse response) {
		//String url = "www.baidu.com";
		if (url != null && !"".equals(url)) {
			ServletOutputStream stream = null;
			try {
				int width = 200;
				int height = 200;
				stream = response.getOutputStream();
				QRCodeWriter writer = new QRCodeWriter();
				BitMatrix m = writer.encode(url, BarcodeFormat.QR_CODE, height, width);
				MatrixToImageWriter.writeToStream(m, "png", stream);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			} finally {
				if (stream != null) {
					try {
						stream.flush();
						stream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			}
		}
	}

6.生成二维码显示的页面shareQrCode.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>分享二维码</title>
    <link rel="stylesheet" href="/static/common/steps/css/bootstrap.min.css">
    <script src="/static/jquery-1.10.2.min.js" charset="UTF-8"></script>
</head>
<body>
    <div class="container" style="width: 356px">
        <div class="starter-template">
            <h1>请用手机浏览器扫码</h1>
        </div>
    </div>
    <div class="container" style="width: 228px">
        <img alt="qrcode" id="qrcode"   src="/count/compositeSearch/getErWeiCode?url=${url}"/>

    </div>
<div class="container" style="width: 346px">
    <footer>
        <p>
            <a href="http://www.****.com" target="_blank">****有限公司 </a> 版权所有 ©2010-2014 
        </p>
    </footer>
</div>
</body>
</html>

7.生成的效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值