工具類,鏈接: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的代碼
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
QrCodeUtils qrCodeUtils = new QrCodeUtils();
//二維碼寬度 二維碼高度 二維碼Logo寬度 二維碼Logo高度 二維碼Logo邊框顏色
qrCodeUtils.initQrCodeUtils(300, 300, 50, 50, new Color(255,0,0));
//二維碼的內容 是否有Logo Logo的路徑
//返回值是一個BufferedImage類型的 這個是個二維碼描出來的內容
BufferedImage QrCodeImage = qrCodeUtils.createQrCodeImage("這個是內容也可以是網頁地址,打開網頁地址需要看瀏覽器支不支持",
//是否有Logo
true,
//Logo地址
"D:\\Desktop\\javaweb\\QrCode\\WebContent\\t.jpg");
ImageIO.write(QrCodeImage, "jpg", response.getOutputStream());
}
Jsp頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<img alt="" src="qrcode">
</body>
</html>
效果:
不帶Logo的二維碼
java代碼
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
QrCodeUtils qrCodeUtils = new QrCodeUtils();
//二維碼寬度 二維碼高度 二維碼Logo寬度 二維碼Logo高度 二維碼Logo邊框顏色
qrCodeUtils.initQrCodeUtils(300, 300, new Color(255,0,0));
//二維碼的內容 是否有Logo Logo的路徑
//返回值是一個BufferedImage類型的 這個是個二維碼描出來的內容
BufferedImage QrCodeImage = qrCodeUtils.createQrCodeImage("這個是內容也可以是網頁地址,打開網頁地址需要看瀏覽器支不支持",
//是否有Logo
false,
//Logo地址
"");
ImageIO.write(QrCodeImage, "jpg", response.getOutputStream());
}
jsp的代碼都是一樣的 這裏就不在寫了
注:Tomcat要使用8.5.x以上的,我也不知道爲什麼Tomcat8.5.x以下的會出現問題
有問題可以在下方評論,如果上面鏈接沒有資源,在下方評論