vue使用qrcode-decoder解析二维码

项目地址

安装

npm install qrcode-decoder --registry=https://registry.npm.taobao.org

封装为函数

// 引入qrcode-decoder
import QrCode from 'qrcode-decoder'

// 传入file对象,返回promise
export function getQrUrl(file) {
  // 获取临时路径 chrome有效,其他浏览器的方法请自行查找
  const url = window.webkitURL.createObjectURL(file)
  // 初始化
  const qr = new QrCode()
  // 解析二维码,返回promise
  return qr.decodeFromImage(url)
}

vue组件中调用

使用了emement的upload组件

<template>
  <el-upload
    action=""
    :show-file-list="false"
    :http-request="resolveQR"
  >
    <el-button
      type="success"
    >上传</el-button>
  </el-upload>
</template>
methods: {
    resolveQR(event) {
      const result = getQrUrl(event.file)
      result.then(res => {
        if (res.data) {
          console.log(res.data)
          this.$message.success('识别二维码成功!')
        } else {
          this.$message.error('识别二维码失败, 请重新上传')
        }
      }).catch(err => {
        this.$message.error(JSON.stringify(err))
      })
    }
  }
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值