工具类,链接:https://pan.baidu.com/s/18U399fTH5wBJPnL97pAekg 提取码:bmw7
注:里面的corejar包是使用的zxing的代码,我只是将其导出的jar文件
二维码大部分是在JavaWeb中使用的,我就在JavaWeb演示了
导入jar,导入后会自动构建jar包
 
  
 
 初始化方法参数解释:
第一个参数:生成二维码的宽度
第二个参数:生成二维码的高度
第三个参数:生成二维码Logo的宽度
第四个参数:生成二维码Logo的高度
第五个参数:生成二维码Logo的边框颜色
initQrCodeUtils(int,int,int,int,Color);
===================================================
第一个参数:生成二维码的宽度
第二个参数:生成二维码的高度
第三个参数:生成二维码Logo的边框颜色
initQrCodeUtils(int,int,Color);
===================================================
第一个参数:生成二维码的内容
第二个参数:是否有Logo 有为true 没有为false
第三个参数:生成二维码Logo的路径
createQrCodeImage(content, logoSwitch, smallLogoPath)
代码:
带有Logo的代码
 1 @Override
 2     protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 3         request.setCharacterEncoding("UTF-8");
 4         response.setCharacterEncoding("UTF-8");
 5         QrCodeUtils qrCodeUtils = new QrCodeUtils();
 6         //二维码宽度      二维码高度     二维码Logo宽度     二维码Logo高度     二维码Logo边框颜色
 7         qrCodeUtils.initQrCodeUtils(300, 300, 50, 50, new Color(255,0,0));
 8         //二维码的内容     是否有Logo  Logo的路径
 9         //返回值是一个BufferedImage类型的                                                          这个是个二维码描出来的内容
10         BufferedImage QrCodeImage = qrCodeUtils.createQrCodeImage("这个是内容也可以是网页地址,打开网页地址需要看浏览器支不支持",
11                 //是否有Logo
12                 true, 
13                 //Logo地址
14                 "D://Desktop//javaweb//QrCode//WebContent//t.jpg");
15         ImageIO.write(QrCodeImage, "jpg", response.getOutputStream());
16     }
 
 Jsp页面:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <img alt="" src="qrcode"> 11 </body>
 
 
不带Logo的二维码
java代码
 1 @Override
 2     protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 3         request.setCharacterEncoding("UTF-8");
 4         response.setCharacterEncoding("UTF-8");
 5         QrCodeUtils qrCodeUtils = new QrCodeUtils();
 6         //二维码宽度      二维码高度     二维码Logo宽度     二维码Logo高度     二维码Logo边框颜色
 7         qrCodeUtils.initQrCodeUtils(300, 300, new Color(255,0,0));
 8         //二维码的内容     是否有Logo  Logo的路径
 9         //返回值是一个BufferedImage类型的                                                          这个是个二维码描出来的内容
10         BufferedImage QrCodeImage = qrCodeUtils.createQrCodeImage("这个是内容也可以是网页地址,打开网页地址需要看浏览器支不支持",
11                 //是否有Logo
12                 false, 
13                 //Logo地址
14                 "");
15         ImageIO.write(QrCodeImage, "jpg", response.getOutputStream());
16     }
 
 jsp的代码都是一样的 这里就不在写了
 
 
注:Tomcat要使用8.5.x以上的,我也不知道为什么Tomcat8.5.x以下的会出现问题
有问题可以在下方评论,如果上面链接没有资源,在下方评论