当前位置:首页 > VUE

vue实现扫码条码

2026-02-21 11:23:25VUE

使用 Vue 实现扫码条码功能

安装依赖库

推荐使用 vue-barcode-readerquagga 库实现扫码功能。安装命令如下:

vue实现扫码条码

npm install vue-barcode-reader quagga

使用 vue-barcode-reader 实现

该库封装了浏览器的 Barcode Detection API,适用于简单场景:

vue实现扫码条码

<template>
  <div>
    <video ref="video" width="300" height="200" autoplay></video>
    <button @click="startScan">开始扫描</button>
    <p>扫描结果: {{ result }}</p>
  </div>
</template>

<script>
import { BarcodeDetector } from 'vue-barcode-reader';

export default {
  data() {
    return {
      result: '',
      detector: null
    };
  },
  methods: {
    async startScan() {
      this.detector = new BarcodeDetector({ formats: ['ean_13'] });
      const stream = await navigator.mediaDevices.getUserMedia({ video: true });
      this.$refs.video.srcObject = stream;

      setInterval(async () => {
        const barcodes = await this.detector.detect(this.$refs.video);
        if (barcodes.length > 0) {
          this.result = barcodes[0].rawValue;
          stream.getTracks().forEach(track => track.stop());
        }
      }, 1000);
    }
  }
};
</script>

使用 Quagga 实现

Quagga 支持更多条码格式且兼容性更好:

<template>
  <div>
    <div id="interactive" class="viewport"></div>
    <button @click="stopScan">停止扫描</button>
    <p>结果: {{ decodedText }}</p>
  </div>
</template>

<script>
import Quagga from 'quagga';

export default {
  data() {
    return {
      decodedText: ''
    };
  },
  mounted() {
    Quagga.init({
      inputStream: {
        name: "Live",
        type: "LiveStream",
        target: document.querySelector('#interactive'),
        constraints: {
          width: 480,
          height: 320,
          facingMode: "environment"
        }
      },
      decoder: {
        readers: ["ean_reader"]
      }
    }, err => {
      if (err) console.error(err);
      Quagga.start();
    });

    Quagga.onDetected(result => {
      this.decodedText = result.codeResult.code;
    });
  },
  methods: {
    stopScan() {
      Quagga.stop();
    }
  }
};
</script>

<style>
.viewport {
  width: 480px;
  height: 320px;
  margin: 0 auto;
}
</style>

注意事项

  • 浏览器兼容性:Chrome 和 Edge 对 Barcode Detection API 支持较好,移动端需测试兼容性
  • HTTPS 环境:摄像头调用需要安全上下文
  • 性能优化:扫描间隔建议设置在 500ms-1000ms 之间
  • 错误处理:添加用户拒绝摄像头权限的提示处理

移动端适配建议

对于移动端项目,可增加全屏模式和方向锁定:

.viewport {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 9999;
}

以上两种方案均可实现基础扫码功能,根据项目需求选择合适方案。Quagga 支持更多格式(如 Code128、QR Code 等),而 vue-barcode-reader 更轻量。

标签: 条码vue
分享给朋友:

相关文章

vue路由实现内部切换

vue路由实现内部切换

Vue 路由实现内部切换的方法 Vue Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-lin…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue 实现直播

vue 实现直播

Vue 实现直播的基本方法 在Vue中实现直播功能,通常需要结合WebRTC、RTMP或HLS等技术。以下是几种常见的实现方式: 使用WebRTC实现实时直播 WebRTC适合低延迟的实时直播场景…

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <templat…

vue实现批量

vue实现批量

Vue 实现批量操作的方法 在 Vue 中实现批量操作通常涉及选择多个项目并执行统一处理,例如批量删除、批量更新等。以下是几种常见实现方式: 表格多选批量操作 使用 el-table 配合复选框实现…