vue使用qrcodejs绘制二维码(带中心logo)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue使用qrcodejs绘制二维码</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/qrcodejs/1.0.0/qrcode.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6/dist/vue.min.js"></script>
    <style>
        .qrCode {
            border-radius: 10px;
            box-shadow: 0 0 2px 3px #eeeeee;
            padding: 10px;
            margin: 10px auto;
        }
        
        .title {
            text-align: center;
        }
        
        .qrCodeContainer {
            margin: auto;
            width: 300px;
            height: 300px;
        }
        /* 重写二维码尺寸 */
        
        .qrCodeContainer canvas+img {
            width: 100%;
            height: 100%;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="qrCode" v-for="(item, index) in cardId" :key="index">
            <p class="title" v-text="item"></p>
            <div class="qrCodeContainer" :ref="'qrCodeDiv'+index"></div>
        </div>
    </div>

    <script>
        new Vue({
            el: '#app',

            data: {
                cardId: ['Cheryl71', 'Daaayyy71']
            },
            mounted() {
                this.$nextTick(() => {
                    for (let i = 0; i < this.cardId.length; i++) {
                        this.creatQrCodeArray(i, this.cardId[i])
                    }
                })
            },
            methods: {
                creatQrCodeArray(index, cardId) {
                    let key = 'qrCodeDiv' + index;
                    let tmp = document.createElement('div');
                    let qrcode = new QRCode(tmp, {
                        text: cardId,
                        width: 162,
                        height: 162,
                        colorDark: "#333333", //二维码颜色
                        colorLight: "#ffffff", //二维码背景色
                        correctLevel: QRCode.CorrectLevel.L //容错率,L/M/H
                    })
                    let logo = new Image();
                    logo.crossOrigin = 'Anonymous';
                    logo.src = "https://img2.baidu.com/it/u=3765512519,3278388264&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500"
                    let that = this;
                    let con = this.$refs[key][0];
                    logo.onload = function() {
                        let container = con;
                        let qrImg = qrcode._el.getElementsByTagName('img')[0];
                        let canvas = qrcode._el.getElementsByTagName('canvas')[0];
                        var ctx = canvas.getContext("2d");
                        ctx.drawImage(logo, 162 * 0.5 - 22, 162 * 0.5 - 22, 44, 44);
                        qrImg.src = canvas.toDataURL();
                        container.appendChild(qrcode._el);
                        // 销毁临时dom
                        tmp = null;
                        qrcode = null;
                        logo = null;
                    }

                }
            }
        })
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值