java後臺生成二維碼,並在前端網頁上顯示
我做的是在網站內容詳情頁下點擊微信分享,需要彈出二維碼,話不多說,直接看效果:
1
首先在前端頁面定義二維碼容器,用來存放後臺生成的二維碼(下面是對應上圖中的三個logo圖標,放在這裏是爲了讓大家看得更清楚!)
<ul>
<li><img src="/static/Images/HitArea/logo-sina.png" alt="" onclick="shareToWeiBo()"></li>
<li><img src="/static/Images/HitArea/logo-friendCircle.png" alt="" onclick="WeiXin()"></li>
<li><img src="/static/Images/HitArea/logo-QQzone.png" alt="" onclick="qqZoneShare()"></li>
</ul>
1
2
3
4
5
`<!-- 存放二維碼的容器 -->
<img id="qrcode" style="padding-left: 20px">`
1
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();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
後臺代碼,首先在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爲圖片高度和寬度
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
編寫工具類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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
另外,因爲jar用的是google的,所以要在pom.xml裏導入相關依賴,這裏我也幫你們準備好了! 在標籤裏粘貼下面兩個即可:
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.2</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
做完這些就可以測試啦,有什麼問題可以私聊我哦!
作者:Shuang_j
來源:CSDN
原文:https://blog.csdn.net/weixin_43601099/article/details/91493485
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!