二维码插件qrcode:
JS逻辑实现:
<!-- 二维码插件,qrcode.min.js下载请百度搜索即可 -->
<script type="text/javascript" src="../js/qrcode.min.js"></script>
<script type="text/javascript">
var qrcode;
window.onload = function() {
qrcode = new QRCode(document.getElementById("qrcode"), {
text : "http://192.168.46.218:8080/hpl/main.html?uid=56",
width : 128,
height : 128,
colorDark : "#0000ff",
colorLight : "#ffffff",
correctLevel : QRCode.CorrectLevel.H
});
}
function download(qrcodeUrl, qrcodeName) {
qrcode.clear();
qrcode.makeCode(qrcodeUrl);
setTimeout(function() {
var img = document.getElementById('qrcode').getElementsByTagName(
'img')[0];
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0);
var url = canvas.toDataURL('image/png');
var a = document.getElementById('download');
a.setAttribute('href', url);
a.setAttribute('download', qrcodeName + '二维码.png');
a.click();
}, 100);
}
</script>
html展示与下载调用部分:
<!-- style="display: none"表示二维码不展示,我们需要二维码时需要去除该属性 -->
<div id="qrcode" style="display: none"></div>
<!-- 下载二维码,注意传入的分别为qrcodeUrl, qrcodeName表示二维码打开链接与二维码下载名称 -->
<a class="btn btn-default btn-xs"
href="javascript:download('http://192.168.46.218:8080/hpl/user/detail?id=${bean.id }','${bean.username }')">详情</a>
图片信息展示如下: