首页 > 其他 > 详细

给通过canvas生成的二维码添加logo

时间:2018-02-02 18:04:08      阅读:327      评论:0      收藏:0      [点我收藏+]

以jquery.qrcode为例来说,

生成二维码代码:

依赖jquery.js, jquery.qrcode.js

//计算宽,高,中心坐标,logo大小                                                                       
 var width = 256,height = 256;                                                                     
 var  x = width * 0.4,
    y = height * 0.4,
    lx = width * 0.2, 
    ly = height * 0.2; 
    $(‘#div‘)
        .qrcode({
            render : "canvas",
            width: width,                                                                     
            height: height,
            text: ‘this is content’
        })
        .hide();   
    var canvas = $("#div canvas")[0];
    //添加logo
    canvas.getContext(‘2d‘).drawImage($("#qrCodeIco")[0], x, y, lx, ly);
    //将canvas生成的二维码内容添加到img里,使得移动端能长按识别二维码
    $(‘#image‘).attr(‘src‘, canvas.toDataURL(‘image/png‘));
<div id="div"></div>
<img id="qrCodeIco" src="logo.png" style="display:none;"/>
<img id="image"  src="" alt="code"/>

 

给通过canvas生成的二维码添加logo

原文:https://www.cnblogs.com/ryans/p/8406096.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!