vue 二维码解码器

本篇是二维码解码器, 生成二维码教程请看另一篇

https://blog.csdn.net/qq_37816525/article/details/97928960

首先安装 qrcode-decoder

npm install qrcode-reader

然后在组件中

<template>
    <div class="module-content">
        <!--解析二维码-->
        <h1 class="module-head">解析二维码</h1>
        <div class="box" id="analytic" style="position: relative;">
            <h3>长按二维码解析</h3>
            [外链图片转存失败(img-rmp7uDdM-1562141512342)(https://mp.csdn.net/mdeditor/tyfo.png)]
            <p class="url-box" id="urlBox"></p>
        </div>
        <div class="box">
            <h3>上传二维码解析</h3>
            <div style="position: relative;">
                [外链图片转存失败(img-jDL9fkbI-1562141512350)(https://mp.csdn.net/mdeditor/tu.png)]
                <input type="file" @change="getUrl('file-url')" name="" id="file" value=""/>
                <p class="url-box"></p>
            </div>
        </div>
    </div>
</template>

<script>
    import QrcodeDecoder from 'qrcode-decoder';


    export default {
        name: "qrcode",
        methods: {
            getUrl(type) {
                let elem = document.getElementById('file');
                console.log(elem);
                let qr = new QrcodeDecoder();
                let url = null;
                if (type === 'img-url') {
                    url = elem.src;
                } else if (type === 'file-url' && elem.files.length > 0) {
                    url = this.getObjectURL(elem.files[0]);
                }

                qr.decodeFromImage(url).then((res) => {
				//打印结果为 解析出来的 二维码地址
                    console.log(res.data);
                });

            },
            getObjectURL(file) {
                let url = null;

                if (window.createObjectURL !== undefined) { // basic

                    url = window.createObjectURL(file);

                } else if (window.URL !== undefined) { // mozilla(firefox)

                    url = window.URL.createObjectURL(file);

                } else if (window.webkitURL !== undefined) { // webkit or chrome

                    url = window.webkitURL.createObjectURL(file);
                }

                return url;
            }

        }

    }
</script>

<style scoped>
    body {
        margin: 0;
    }

    .module-content {
        min-width: 320px;
        max-width: 1000px;
        width: 100%;
        color: #000;
        margin: 0 auto;
        padding-left: 10px;
        box-sizing: border-box;
    }

    .module-head {
        text-align: center;
        font-weight: 500;
        margin: 0;
        font-size: 30px;
        height: 100px;
        line-height: 100px;
        color: #000;
    }

    .box h3 {
        font-weight: 300;
        margin: 0;
        font-size: 20px;
        height: 60px;
        line-height: 60px;
        color: #000;
    }

    .url-box {
        height: 30px;
        line-height: 30px;
        font-size: 14px;
    }

    #file {
        position: absolute;
        width: 120px;
        height: 120px;
        opacity: 0;
        top: 0;
        left: 0;
        overflow: hidden;
        z-index: 10;
    }
</style>
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值