vue实现图片和生成二维码的合成-创新互联
                                            本文主要讲述生成二维码并合并二维码与图片的功能。

第一步:生成二维码图片
安装:yarn add qrcanvas --save
组件中引入:import { qrcanvas } from 'qrcanvas';
组件使用:
    
        
             
            
        
    
script中代码:
let that = this;
that.appSrc = "www.baidu.com";
that.$nextTick(function () {
        //生成二维码
        var canvas1 = qrcanvas({
                data: decodeURIComponent(that.appSrc),
                size:80
        });
        document.getElementById("qrcode").innerHTML = '';
        document.getElementById('qrcode').appendChild(canvas1);
});第二步:合成二维码和图片
安装:yarn add html2canvas --save
组件中引入:import html2canvas from 'html2canvas';
组件使用:
    
        ![分享图片]() 
    
script中代码:
let that = this;
that.appSrc = "www.baidu.com";
that.$nextTick(function () {
        //合成分享图
        html2canvas(that.$refs.box).then(function(canvas) {
                //错误写法,此处会触发坑二
                //that.imgUrl =  URL.createObjectURL(that.base64ToBlob(canvas.toDataURL())) 
                //正确写法
                let dataURL = canvas.toDataURL("image/png");
                that.imgUrl = dataURL;
        });
});方法base64ToBlob:
base64ToBlob(code) {
        let parts = code.split(';base64,');
        let contentType = parts[0].split(':')[1];
        let raw = window.atob(parts[1]);
        let rawLength = raw.length;
        let uInt8Array = new Uint8Array(rawLength);
        for (let i = 0; i < rawLength; ++i) {
                uInt8Array[i] = raw.charCodeAt(i);
        }
        return new Blob([uInt8Array], {type: contentType});
}坑一:使用canvas生成二维码以便之后实现合成
坑二:生成的图片需要转化为base64格式才能保存完整代码呈现:
    
        ![分享图片]() 
        
            ![]() 
            
        
    
script中代码:
let that = this;   
that.appSrc = "www.baicu.com";
that.$nextTick(function () {
        //生成二维码
        var canvas1 = qrcanvas({
                data: decodeURIComponent(that.appSrc),
                size:80
        });
        document.getElementById("qrcode").innerHTML = '';
        document.getElementById('qrcode').appendChild(canvas1);
        //合成分享图
        html2canvas(that.$refs.box).then(function(canvas) {
                let dataURL = canvas.toDataURL("image/png");
                that.imgUrl = dataURL;
        });
});另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站题目:vue实现图片和生成二维码的合成-创新互联
当前URL:http://www.scyingshan.cn/article/dssdeg.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 