当前位置:首页 > VUE

vue实现扫码

2026-01-08 06:50:06VUE

Vue 实现扫码功能

使用 vue-qrcode-reader

安装 vue-qrcode-reader 库:

npm install vue-qrcode-reader

在 Vue 组件中引入并使用:

vue实现扫码

<template>
  <div>
    <qrcode-stream @decode="onDecode"></qrcode-stream>
    <p>扫描结果: {{ result }}</p>
  </div>
</template>

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

export default {
  components: { QrcodeStream },
  data() {
    return {
      result: ''
    }
  },
  methods: {
    onDecode(decodedString) {
      this.result = decodedString
    }
  }
}
</script>

使用 html5-qrcode

安装 html5-qrcode 库:

npm install html5-qrcode

在 Vue 组件中实现扫码:

vue实现扫码

<template>
  <div>
    <div id="qr-reader" style="width: 500px"></div>
    <p>扫描结果: {{ result }}</p>
  </div>
</template>

<script>
import { Html5Qrcode } from 'html5-qrcode'

export default {
  data() {
    return {
      result: ''
    }
  },
  mounted() {
    const html5Qrcode = new Html5Qrcode('qr-reader')
    html5Qrcode.start(
      { facingMode: 'environment' },
      { fps: 10, qrbox: 250 },
      (decodedText) => {
        this.result = decodedText
      },
      (errorMessage) => {
        console.error(errorMessage)
      }
    )
  }
}
</script>

使用原生 API 实现

通过浏览器原生 API 调用摄像头并解析二维码:

<template>
  <div>
    <video ref="video" width="400" height="300" autoplay></video>
    <canvas ref="canvas" width="400" height="300" style="display: none"></canvas>
    <button @click="scanQR">开始扫描</button>
    <p>扫描结果: {{ result }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      result: '',
      interval: null
    }
  },
  methods: {
    async scanQR() {
      const stream = await navigator.mediaDevices.getUserMedia({ video: true })
      this.$refs.video.srcObject = stream

      this.interval = setInterval(() => {
        const canvas = this.$refs.canvas
        const context = canvas.getContext('2d')
        context.drawImage(this.$refs.video, 0, 0, canvas.width, canvas.height)
        const imageData = context.getImageData(0, 0, canvas.width, canvas.height)
        // 这里需要添加二维码解析逻辑
        // 可以使用第三方库如 jsQR 来解析
      }, 500)
    }
  },
  beforeDestroy() {
    if (this.interval) clearInterval(this.interval)
    if (this.$refs.video.srcObject) {
      this.$refs.video.srcObject.getTracks().forEach(track => track.stop())
    }
  }
}
</script>

移动端适配

对于移动端设备,需要考虑权限请求和横竖屏适配:

<template>
  <div class="scanner-container">
    <qrcode-stream
      @decode="onDecode"
      :torch="torchActive"
      @torch-compatibility="onTorchCompatibility"
    ></qrcode-stream>
    <button @click="toggleTorch">切换闪光灯</button>
  </div>
</template>

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

export default {
  components: { QrcodeStream },
  data() {
    return {
      result: '',
      torchActive: false,
      hasTorch: false
    }
  },
  methods: {
    onDecode(decodedString) {
      this.result = decodedString
    },
    onTorchCompatibility({ compatible }) {
      this.hasTorch = compatible
    },
    toggleTorch() {
      if (this.hasTorch) {
        this.torchActive = !this.torchActive
      }
    }
  }
}
</script>

<style>
.scanner-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
</style>

以上方法提供了从简单到复杂的多种实现方案,可以根据项目需求选择合适的方案。对于生产环境,推荐使用成熟的第三方库如 vue-qrcode-readerhtml5-qrcode,它们已经处理了大多数兼容性和性能问题。

标签: vue
分享给朋友:

相关文章

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed: {…

vue实现下拉框删除

vue实现下拉框删除

Vue 实现下拉框删除功能 在Vue中实现下拉框的删除功能通常涉及以下步骤: 数据绑定与渲染 在Vue组件中定义数据数组用于存储下拉框选项: data() { return { op…

vue实现treeview

vue实现treeview

Vue 实现 TreeView 的方法 使用递归组件实现 TreeView 递归组件是 Vue 中实现 TreeView 的常见方式。通过组件调用自身,可以轻松处理嵌套的树形结构数据。 <te…

vue实现api

vue实现api

Vue 实现 API 调用 在 Vue 中调用 API 通常涉及使用 axios 或 fetch 发送 HTTP 请求,并结合 Vue 的响应式特性管理数据。以下是常见的实现方式: 安装 axios…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…

轮播vue实现

轮播vue实现

使用 Vue 实现轮播功能 基础轮播实现 安装依赖(如使用 Swiper): npm install swiper vue-awesome-swiper 组件代码示例: <template&…