function qrcodeSynthesis(obj,setting){
return new Promise(function(resolve,reject){
var canvas = document.createElement("canvas");
//设置canvas宽高
canvas.width = setting.width;
canvas.height = setting.height;
//获取绘图环境
var context = canvas.getContext('2d');
if(!context){
alert('您的浏览器尚不支持,请更换浏览器');
return ;
}
//设置加载图片的状态
var ImgStatus = {
qrImgStatus:false
}
//设置二维码
var qrImg = new Image();
//加载二维码
qrImg.onload = function(){
//绘制二维码
context.drawImage(qrImg,0,0);
ImgStatus.qrImgStatus = true;
}
//填充画布背景
drawRect(context,0,0,setting.width,setting.height,setting.canvasBorder[0],setting.canvasBorder[1],setting.canvasBg);
//绘制二维码
qrImg.crossOrigin = "Anonymous" ; //二维码
qrImg.src = obj.qrcode;
//绘制编号
var style ={
font:'bold 16px Arial ',
color:'#000',
textBaseline:'middle'
};
var numTxt = {
txt:obj['label0']+' :'+obj['key0'],
x:setting.qrcode.width,
y:40
}
var numTxtAttr = drawText(context,numTxt,style);
//绘制资产名称
var nameTxt = {
txt:obj['label1']+' :'+obj['key1'],
x:setting.qrcode.width,
y: numTxt.y + numTxtAttr.txtHeight + 15
}
var nameTxtAttr = drawText(context,nameTxt,style);
(function imgStatus(ImgStatus){
var time = null;
if(ImgStatus.qrImgStatus){
var imgdate = canvas.toDataURL("image/jpeg", 0.5);
resolve(imgdate);
return false;
}else{
time = setTimeout(function(){
imgStatus(ImgStatus);
},1000);
}
})(ImgStatus);
})
//绘制矩形
function drawRect(cxt,x,y,width,height,BW,BC,fillColor){
cxt.beginPath();
cxt.moveTo(x,y);
cxt.lineTo(x+width,y);
cxt.lineTo(x+width,y+height);
cxt.lineTo(x,y+height);
cxt.closePath();
cxt.lineWidth=BW;
cxt.fillStyle=fillColor;
cxt.strokeStyle=BC;
cxt.fill();
cxt.stroke();
}
//绘制单行文本
function drawText(cxt,obj,setobj){
var setting = {
font:'30px Arial',
color:'#000',
textBaseline:'middle'
}
if(setobj) setting = Object.assign({},setting,setobj);
cxt.font=setting.font;
cxt.fillStyle=setting.color;
cxt.fillText(obj.txt,obj.x,obj.y);
let fontHeight = setting.font.match(/\d+/);
return {
txt:obj.txt,
txtWidth:cxt.measureText(obj.txt).width,
txtHeight: parseInt(fontHeight[0]),
source:obj
}
}
}