jquery生成二维码怎么添加图片(两种方法)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/flting1017/article/details/79089292
自动生成二维码里面添加图片,有两种方法,一种是使用jquery原生的二维码插件,一种是别人有改动过的二维码插件,生成二维码的图片的时候有那么一些写的差别
第一种:使用的是原生的qrcode.js生成二维码加上图片
页面
<div class="margin-top-10" id="wechartauto">
<img id="qrCodeIco"/>
</div>
js
this.$http.post('/customer/ajax',{method:'getqrcode'},{emulateJSON:true}).then(function(response){
var wechartUlr = response.data.data;
// console.log(wechartUlr)
$('#wechartauto').qrcode({
render: "canvas", // 渲染方式有table方式(IE兼容)和canvas方式
width: 155, //宽度
height: 155, //高度
text: wechartUlr, //内容
typeNumber: -1,//计算模式
correctLevel: 2,//二维码纠错级别
background: "#ffffff",//背景颜色
foreground: "#000000" //二维码颜色
});
//设置图片的大小
$("#qrCodeIco").css("width","50px")
$("#qrCodeIco").css("height","50px")
$("#qrCodeIco").attr("src","/images/newdashboard/wechat.png")
var margin = ($("#wechartauto").height() - $("#qrCodeIco").height()) / 2; //控制Logo图标的位置
$("#qrCodeIco").css("margin", margin);
})
css
canvas{
padding:10px;border:1px solid #f5f5f5;box-shadow:0px 1px 1px #e5e5e5;
}
成果

第二种方法,就是使用有改动过的qrcode.js插件
<!--此处需要引入三个JS文件
一、jquery-1.8.2.js
二、excanvas.js
三、qrcode.js
顺序要注意,不能乱了顺序;
-->
<script src="js/jquery-1.8.2.js" type="text/javascript"></script>
<script src="js/excanvas.js"></script>
<script src="js/qrcode.js"></script>
<script type="text/javascript">
$(function() {
$("#bt").bind("click",
function() {
$("#qrcode_div").empty();
var text = $("#text").val();
$('#qrcode_div').qrcode({
render: 'canvas',
text: utf16to8(text),
height: 200,
width: 200,
typeNumber: -1, //计算模式
correctLevel: QRErrorCorrectLevel.M,//纠错等级
background: "#ffffff", //背景颜色
foreground: "#000000", //前景颜色
//logo图片地址
src: 'logo.png'

});
//console.info("wwww");
}
);
});

function utf16to8(str) { //转码
var out, i, len, c;
out = "";
len = str.length;
for (i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += str.charAt(i);
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}

<input type="text" id="text" value="这是输入中文去解析二维码" style="width:200px;"/>
<input type="button" value="生成二维码" id="bt" />
<div id="qrcode_div" style="margin-top:10px;">

此方法得出的效果是一样的

后续补上实现的代码,会放在github上面








猜你喜欢

转载自blog.csdn.net/flting1017/article/details/79089292
今日推荐