vue在调用摄像头扫码(vue-qrcode-reader)

1. vue-qrcode-reader

在浏览器端html中有一款依赖可以直接调用摄像头扫码,vue-qrcode-reader
分别提供QrcodeStreamQrcodeDropZoneQrcodeCapture用来识别二维码。

  1. QrcodeStream: 调用摄像头扫码
  2. QrcodeDropZone:将图片移到识别区域内识别二维码
  3. QrcodeCapture:选择原生拍照或者图片到浏览器识别二维码。

2. 安装

npm install vue-qrcode-reader

3. QrcodeStream直接调用摄像头扫码

  1. init方法是初始化调用摄像头,此时如果摄像头报错会有很多提示,请酌情处理
  2. decode方法,当识别到二维码会把识别信息触发出来
<template>
  <div>
    <p class="error">{{ error }}</p>

    <p class="decode-result">Last result: <b>{{ result }}</b></p>

    <qrcode-stream @decode="onDecode" @init="onInit" />
  </div>
</template>

<script>
import { QrcodeStream } from 'vue-qrcode-reader'

export default {

  components: { QrcodeStream },

  data () {
    return {
      result: '',
      error: ''
    }
  },

  methods: {
    onDecode (result) {
      this.result = result
    },

    async onInit (promise) {
      try {
        await promise
      } catch (error) {
        if (error.name === 'NotAllowedError') {
          this.error = "ERROR: you need to grant camera access permission"
        } else if (error.name === 'NotFoundError') {
          this.error = "ERROR: no camera on this device"
        } else if (error.name === 'NotSupportedError') {
          this.error = "ERROR: secure context required (HTTPS, localhost)"
        } else if (error.name === 'NotReadableError') {
          this.error = "ERROR: is the camera already in use?"
        } else if (error.name === 'OverconstrainedError') {
          this.error = "ERROR: installed cameras are not suitable"
        } else if (error.name === 'StreamApiNotSupportedError') {
          this.error = "ERROR: Stream API is not supported in this browser"
        } else if (error.name === 'InsecureContextError') {
          this.error = `ERROR: Camera access is only permitted in secure context. 
          Use HTTPS or localhost rather than HTTP.`;
        } else {
          this.error = `ERROR: Camera error (${error.name})`;
        }
      }
    }
  }
}
</script>

<style scoped>
.error {
  font-weight: bold;
  color: red;
}
</style>

4. 针对部分IOS【Vue warn】错误

TypeError: Reflect.construct requires the first argument to be a constructor
在这里插入图片描述
我目前没有更好的解决办法,只能充分利用vue生命周期中的捕获错误处理 errorCaptured


有个开发说新版本不支持的
在这里插入图片描述

5. QrcodeCapture用原生拍照或者直接选择图片识别

decode方法和QrcodeStream一致,如果识别到了二维码将触发该事件。

<template>
  <div>
    <p>
      Capture:
      <select v-model="selected">
        <option v-for="option in options" :key="option.text" :value="option">
          {{ option.text }}
        </option>
      </select>
    </p>

    <hr/>

    <p class="decode-result">Last result: <b>{{ result }}</b></p>

    <qrcode-capture @decode="onDecode" :capture="selected.value" />
  </div>
</template>

<script>
import { QrcodeCapture } from 'vue-qrcode-reader'

export default {

  components: { QrcodeCapture },

  data () {
    const options = [
      { text: "rear camera (default)", value: "environment" },
      { text: "front camera", value: "user" },
      { text: "force file dialog", value: false },
    ]

    return {
      result: '',
      options,
      selected: options[0]
    }
  },

  methods: {
    onDecode (result) {
      this.result = result
    }
  }
}
</script>

注意:选择图片识别的识别率不是很高,但是已经是planB了,可以试试更好的planB或者让作者再努力一下。




完整demo

  • 9
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 20
    评论
### 回答1: vue-qrcode-reader是一个基于Vue.js的条形码扫描组件。它为用户提供了在网页上扫描条形码的功能。 使用vue-qrcode-reader,我们需要先安装并导入组件。然后,我们可以在Vue的模板中使用组件,并通过设定相应的参数来调整扫描的设置。 该组件支持从摄像头扫描条形码,并可以将扫描到的条形码数据在网页展示出来。我们可以通过捕获组件的事件来获取扫描到的条形码数据,并进行后续的处理。 在使用vue-qrcode-reader的过程中,我们可以自定义组件的样式、设置摄像头的类型、调整扫描的精度等。此外,组件还提供了一些其他的参数,如自动开始扫描、扫描完成后是否暂停等,以便根据实际需求进行相应的调整。 使用vue-qrcode-reader可以方便地在Vue项目中实现条形码的扫描功能。它简化了扫描条形码的开发过程,提高了用户体验,并可以应用于各种需要条形码扫描的场景,如电商购物车、门禁系统等。 ### 回答2: vue-qrcode-reader是一个用于Vue.js的条形码扫描器插件。它基于QuaggaJS库,并提供简单易用的接口,让我们能够在Vue.js应用程序中轻松地集成条形码扫描功能。 使用vue-qrcode-reader非常简便。首先,我们需要安装该插件,可以在命令行中运行`npm install vue-qrcode-reader`来进行安装。安装完成后,可以在Vue组件中引入并注册该插件。 在使用vue-qrcode-reader之前,我们需要在页面中准备一个用于显示扫描结果的容器元素。可以使用一个`div`元素,并在其上绑定一个自定义的`class`或`id`,以便在Vue组件中引用。 然后,在Vue组件中,我们需要导入vue-qrcode-reader插件,并在`components`选项中进行注册。然后,我们可以在组件的`template`中使用插件提供的`Scanner`组件,来实现条形码的扫描功能。 在`Scanner`组件中,我们可以使用`@scan-success`事件来监听扫描成功的回调函数,并处理扫描到的条形码数据。我们还可以通过设置`@picker-active-red-line-color`属性来改变扫描框的红色边线颜色,以及使用`@activate-vibrate`属性来设置设备在扫描时是否震动反馈。 最后,在Vue组件中,我们可以使用`this.$refs.scanner.start`方法来启动条形码扫描,使用`this.$refs.scanner.stop`方法来停止条形码扫描。 总结而言,vue-qrcode-reader是一个方便易用的Vue.js条形码扫描器插件,可以轻松地集成到Vue.js应用程序中,实现条形码的扫描功能。 ### 回答3: vue-qrcode-reader 是一款基于 Vue.js 的扫码组件,可以用于扫描条形码。它使用了浏览器的摄像头,通过扫描条形码获取条形码的信息。 使用 vue-qrcode-reader 很简单,只需要在 Vue.js 的项目中导入该组件,并在需要扫描条形码的地方添加该组件即可。通过调用组件的方法,可以解析并获取扫描到的条形码信息。 在使用 vue-qrcode-reader 的时候,可以根据需要进行设置,如配置是否启用闪光灯、设置摄像头的分辨率等。扫描到条形码后,可以通过回调函数获取到扫描到的条形码的内容。 vue-qrcode-reader 提供了一套功能完善的 API,可以方便地进行二维码的扫描和解析。同时,它还支持多种条形码的解析,如 EAN、UPC、Code39 等。 总的来说,vue-qrcode-reader 是一款方便实用的扫码组件,可以在 Vue.js 项目中轻松实现条形码的扫描功能,为用户提供更加便捷的条形码操作体验。
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值