js生成带log的二维码(qrcodejs)

github: qrcodejs

cdn: http://static.runoob.com/assets/qrcode/qrcode.min.js

#qrcode
#qrcode
    margin: 20px
// 生成二维码图片
let tmp = document.createElement('div');
let qrcode = new QRCode(tmp, {
   text: "https://cn.vuejs.org/v2/guide/",
   width: 128,
   height: 128,
   colorDark : "#000000",
   colorLight : "#ffffff",
   useCanvas: true,
   correctLevel : QRCode.CorrectLevel.H
});
// 加载logo
let logo = new Image();
//logo.crossOrigin = 'Anonymous';
//logl.src = 'https://cn.vuejs.org/images/logo.png';
logo.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAMAAAC3Ycb+AAACLlBMVEVMaXFBuINBuIM8enI/nnxBuINBuINBuIM8enJBuIM8enJBuINBuINBuIM8enJBuIM8enJBuINBuIM7eXFBuINBuIM8eXFBuINBuIM8eHFBuIM8eXFBuIM8eHBBuIM8eHBBuIM8d3BBuINBuIM8d3BBuINBuINBuIM8dnBBuINBuIM8dnBBuINBuIM7dXBBuINBuIM8dG9BuINBuIM8dG9BuIM8dG9BuINBuIM8c29BuINBuIM7cm5BuINBuIM7cW5BuIM7cW9BuINBuINBuIM7cW5BuINBuINBuIM7b21BuINBuIM6b21BuIM7bm1BuINBuIM7bW1BuINBuIM6bGxBuINBuIM6a2xBuIM6a2xBuINBuIM6amxBuIM6aWtBuINBuIM6aGpBuINBuIM6ZmpBuINBuIM5ZGlBuIM5Y2lBuINBuIM5YWhBuINBuIM5YGdBuIM4XmdBuINBuIM4XGZBuINBuIM4WmVBuIM3WGRBuIM3V2RBuINBuIM3VGNBuIM2UmI2UGFBuIM1TWA1SV41Sl41S141TF81TWA2T2A2UWE2U2I3VWM3WGQ4W2U4Xmc5YGc5YWg5ZGk6Zmo6Z2o6aGo6a2w7bW07bm07cG48c287dnA7eHA8enI8fHI9fnM8gXM9g3Q9hnU9iXY+i3c+jHc+j3g+kXk+lHo/l3o/mXs+m3s/nnw/oH0/o31Apn5BqH5Aqn9BrH9Bro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YeBvtq4WGwrxgeBvuq4WGwr8DdnNO4Kh4G+4rhYbCvFh4G+2rhYbCvHh7Wx77gYX3sCx7Wx77gYbCvYXcL+6rhYS3sS5uwrzAeFsC+9K+Gfbmba53G6W0M+9IJ7KuGh7WwL63UsC9rbmu31XfYVw0Pa2FfehbDvrRXw77gYS3sSzM17Ase1sK+9DGGfem6hn3Bw1rYlzZq2Bc8rIV96U8Y+7Lm2m0DvYphXzquYV/wsBb2paUa9gUPa2Ff+hbGvtzNOY0L9AT2FWsX9hXGw2BfgaZhX2E8DPYV6APsK9YF7CuMh8G+Aq3BvsJ4GOwr0G/YVxgPg30Fegn7inUI+wrjYbCvQAuwrzAeBvsK9AX21V9z7bYZPAz2FWgb9hXGw2Bfgf7DvsJ4GOwr0DvYV6wz2FcXD4N9FVqFfVXv5pzGNfrZxL7gYbCvSs9hX7EOetgXPAz2VWquhn3Bw2BfrT7DviRJkiRJkiRJkiRJkiRJkiQNszucKAuToL+DiQAAAABJRU5ErkJggg==';

logo.onload = function () {
   let container = document.getElementById('qrcode');
   let qrImg = qrcode._el.getElementsByTagName('img')[0];
   let canvas = qrcode._el.getElementsByTagName('canvas')[0];
   var ctx = canvas.getContext("2d");
   ctx.drawImage(logo, 128 * 0.5 - 20, 128 * 0.5 - 20, 40, 40);
   qrImg.src = canvas.toDataURL();
   container.appendChild(qrcode._el);
   // 销毁临时dom
   tmp = null;
   qrcode = null;
   logo = null;
}

See the Pen js生成带log的二维码 by flqbestboy
(@fanlinqiang) on CodePen.

Canvas画图加载远程图片报错

pen.js:26 Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
       at Image.logo.onload (pen.js:33:22)

没有跨域CORS授权也可以在 canvas 中使用图像, 但这样做就会污染(taints)画布。 只要 canvas 被污染, 就不能再从画布中提取数据, 也就是说不能再调用 toBlob(), toDataURL() 和 getImageData() 等方法, 否则会抛出安全错误(security error).
这实际上是为了保护用户的个人信息,避免未经许可就从远程web站点加载用户的图像信息,造成隐私泄漏。

解决方案:

  1. 允许远程服务器Access-Control-Allow-Origin开启跨域
  2. 图片服务放在同一域名

转载于:https://www.cnblogs.com/fanlinqiang/p/11228080.html

你可能感兴趣的:(js生成带log的二维码(qrcodejs))