js生成二维码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

js⽣成⼆维码
采⽤jquery.qrcode.js
可⽣成带logo图标的⼆维码
宽、⾼、背景⾊、前景⾊可⾃定义设置
html代码如下:
<div class="qrcode">
<img class="qrcode-img" src="" alt="">
</div>
js代码如下:
//⽣成⼆维码
var qrcode = $(".qrcode").qrcode({
render: "canvas",
text: _this.text,
width : "400", //⼆维码的宽度
height : "400", //⼆维码的⾼度
background : "#ffffff", //⼆维码的后景⾊
foreground : "#000000", //⼆维码的前景⾊
src: 'https:///oss/logo.png' //⼆维码中间的图⽚
});
var canvas = qrcode.find('canvas').get(0);
$('.qrcode-img').attr('src', canvas.toDataURL('image/png')).css({
maxWidth:300
});
jquery.qrcode.js代码如下:
function QR8bitByte(a){
this.mode=QRMode.MODE_8BIT_BYTE,
this.data=a
}
function QRCode(a,b){
this.typeNumber=a,
this.errorCorrectLevel=b,
this.modules=null,
this.moduleCount=0,
this.dataCache=null,
this.dataList=new Array()
}
function QRPolynomial(a,b){
var c,d;
if(void 0==a.length){
throw new Error(a.length+"/"+b)
}
for(c=0;c<a.length&&0==a[c];){
c++
}
for(this.num=new Array(a.length-c+b),d=0;d<a.length-c;d++){
this.num[d]=a[d+c]
}
}
function QRRSBlock(a,b){
this.totalCount=a,
this.dataCount=b
}
function QRBitBuffer(){
this.buffer=new Array(),
this.length=0
}
var QRMode,QRErrorCorrectLevel,QRMaskPattern,QRUtil,QRMath,i;
for(function(a){
a.fn.qrcode=function(b){
var c,d;
return"string"==typeof b&&(b={text:b}),
b=a.extend({},{
render:"canvas",
width:256,
height:256,
//杩欓噷鏄 浘鐗囩殑楂樺害鍜屽 搴�
imgWidth:b.width/3,
imgHeight:b.height/3,
typeNumber:-1,
correctLevel:QRErrorCorrectLevel.H,
background:"#ffffff",
foreground:"#000000"
},b),c=function(){
var c,d,e,f,g,h,i,j,k,a=new QRCode(b.typeNumber,b.correctLevel);
for(a.addData(utf16to8(b.text)),a.make(),
c=document.createElement("canvas"),
c.width=b.width,c.height=b.height,d=c.getContext("2d"),
b.src&&(e=new Image(),e.src=b.src,e.onload=function(){
d.drawImage(e,(b.width-b.imgWidth)/2,(b.height-b.imgHeight)/2,
b.imgWidth,b.imgHeight)}),f=b.width/a.getModuleCount(),
g=b.height/a.getModuleCount(),h=0;h<a.getModuleCount();h++)
{for(i=0;i<a.getModuleCount();i++){
d.fillStyle=a.isDark(h,i)?b.foreground:b.background,
j=Math.ceil((i+1)*f)-Math.floor(i*f),
k=Math.ceil((h+1)*f)-Math.floor(h*f),
d.fillRect(Math.round(i*f),Math.round(h*g),j,k)
}}return c},
d=function(){var d,e,f,g,h,i,
c=new QRCode(b.typeNumber,b.correctLevel);
//杩欓噷鐨剈tf16to8(b.text)鏄 Text涓 殑瀛楃 涓茶繘琛岃浆鐮侊紝璁╁叾鏀 寔涓 枃
for(c.addData(utf16to8(b.text)),c.make(),d=a("<table></table>").css("width",b.width+"px").css("height",b.height+"px").css("border","0px").css("border-collapse","collapse").css("background-color",b.background),e=b.width/c.getModuleCount(),f。

相关文档
最新文档