java后台生成二维码,并在前端网页上显示

您所在的位置:网站首页 二维码生成器程序怎么写 java后台生成二维码,并在前端网页上显示

java后台生成二维码,并在前端网页上显示

2024-03-15 23:25| 来源: 网络整理| 查看: 265

我做的是在网站内容详情页下点击微信分享,需要弹出二维码,话不多说,直接看效果:

首先在前端页面定义二维码容器,用来存放后台生成的二维码(下面是对应上图中的三个logo图标,放在这里是为了让大家看得更清楚!)

å¨è¿éæå¥å¾çæè¿°

` `

2.给微信logo添加 onclick() 事件(我的代码都放在对应js文件中,使用时需在HTML页面引入,你们自己也可以直接放在页面的

//js中方法,微信扫描二维码 function WeiXin(){ //debugger; //清空二维码文本框 $("#qrcode").html(""); var title = $("#commentTitle").val(); var url = window.location.href; var url2 = url.split("localhost:8068/"); if (url2.length > 1){ var url3 = url2[1]; var url4 = "http://www.zhengquan51.com/" + url3; } else{ //如果是在线上路径下 url4 = url2; } var icno = $("#icno").val(); if (icno == undefined || icno == null){ icno = ""; } //主要看这里就行了(作用是调用后台接口以及图片回显) $("#qrcode").attr("src", "/getCode/qrcode?content=" + url4); //根据路径访问后台接口,生成二维码并通过src属性展示在容器中,url4为我需要生成二维码的页面链接内容 $(".Index-Popup-Boxs").show(); }

3.后台代码,首先在controller层写qrcode方法,代码如下:

/** * 生成微信图片二维码 * * @param request * @param response * @param content 为前端传过来的二维码的内容,即路径链接 * @throws Exception */ @Log("微信图片二维码") @GetMapping("/qrcode") public void qrcode(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = "content") String content) throws Exception { if (StringUtils.isBlank(content)) { response.sendRedirect("/404.html"); return; } //调用工具类,生成二维码 RecodeUtil.creatRrCode(content, 180,180,response); //180为图片高度和宽度 }

 

4.编写工具类RecodeUtil,该类存放在我项目下的utils文件目录下,你们可以自行选在位置,在controller里导入就行了,工具类代码如下:

import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Hashtable; public class RecodeUtil { public static void creatRrCode(String contents, int width, int height,HttpServletResponse response) { Hashtable hints = new Hashtable(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //容错级别最高 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //设置字符编码 hints.put(EncodeHintType.MARGIN, 1); //二维码空白区域,最小为0也有白边,只是很小,最小是6像素左右 try { BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); // 1、读取文件转换为字节数组 // ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedImage image = toBufferedImage(bitMatrix); //转换成png格式的IO流 ImageIO.write(image, "png", response.getOutputStream()); // byte[] bytes = out.toByteArray(); // // 2、将字节数组转为二进制 // BASE64Encoder encoder = new BASE64Encoder(); // binary = encoder.encodeBuffer(bytes).trim(); } catch (WriterException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * image流数据处理 * * @author ianly */ public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } return image; } }

5.另外,因为jar用的是google的,所以要在pom.xml里导入相关依赖,这里我也帮你们准备好了! 在标签里粘贴下面两个即可:

com.google.zxing core 2.2 com.google.zxing javase 2.2

6.做完这些就可以测试啦,有什么问题可以私聊我哦!

 转载链接 https://blog.csdn.net/weixin_43601099/article/details/91493485



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3