当前位置:首页 > VUE

vue实现扫码解析

2026-01-20 07:45:44VUE

使用 vue-qrcode-reader 库实现扫码

安装 vue-qrcode-reader 库:

npm install vue-qrcode-reader

在 Vue 组件中引入并使用:

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

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

export default {
  components: { QrcodeStream },
  methods: {
    onDecode(decodedString) {
      console.log(decodedString)
      // 处理扫描结果
    }
  }
}
</script>

使用 Html5Qrcode 库实现扫码

安装 html5-qrcode:

npm install html5-qrcode

Vue 组件实现:

<template>
  <div id="qr-reader" style="width: 300px"></div>
</template>

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

export default {
  mounted() {
    const html5QrCode = new Html5Qrcode("qr-reader")
    const config = { fps: 10, qrbox: 250 }

    html5QrCode.start(
      { facingMode: "environment" },
      config,
      (decodedText) => {
        console.log(decodedText)
        // 处理扫描结果
      },
      (errorMessage) => {
        // 处理错误
      }
    )
  }
}
</script>

使用 ZXing 库实现扫码

安装 @zxing/library:

vue实现扫码解析

npm install @zxing/library

Vue 组件实现:

<template>
  <video ref="video" width="300" height="200"></video>
</template>

<script>
import { BrowserMultiFormatReader } from '@zxing/library'

export default {
  data() {
    return {
      codeReader: new BrowserMultiFormatReader()
    }
  },
  mounted() {
    this.codeReader.decodeFromVideoDevice(
      undefined,
      this.$refs.video,
      (result, err) => {
        if (result) {
          console.log(result.text)
          // 处理扫描结果
        }
        if (err && !(err instanceof ZXing.NotFoundException)) {
          console.error(err)
        }
      }
    )
  },
  beforeDestroy() {
    this.codeReader.reset()
  }
}
</script>

实现本地图片二维码解析

使用 vue-qrcode-reader 解析本地图片:

<template>
  <qrcode-capture @decode="onDecode" />
  <input type="file" @change="onFileChange" />
</template>

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

export default {
  components: { QrcodeCapture },
  methods: {
    onDecode(decodedString) {
      console.log(decodedString)
    },
    onFileChange(event) {
      const file = event.target.files[0]
      if (!file) return

      const reader = new FileReader()
      reader.onload = (e) => {
        this.$refs.qrCapture.decode(e.target.result)
      }
      reader.readAsDataURL(file)
    }
  }
}
</script>

扫码功能优化建议

添加摄像头切换功能:

vue实现扫码解析

<template>
  <qrcode-stream 
    @decode="onDecode" 
    :camera="camera"
    @init="onInit"
  />
  <button @click="switchCamera">切换摄像头</button>
</template>

<script>
export default {
  data() {
    return {
      camera: 'auto',
      cameras: []
    }
  },
  methods: {
    onInit(promise) {
      promise.then(({ capabilities }) => {
        this.cameras = capabilities
      })
    },
    switchCamera() {
      this.camera = this.camera === 'auto' ? 'off' : 'auto'
    }
  }
}
</script>

添加扫描框样式美化:

.qr-box {
  border: 2px solid #3eaf7c;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.5);
  position: relative;
  width: 80%;
  max-width: 300px;
  margin: 0 auto;
}

.qr-box::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background: #3eaf7c;
  animation: scan 2s infinite linear;
}

@keyframes scan {
  0% { top: 0 }
  100% { top: 100% }
}

注意事项

确保在移动设备上测试扫码功能,不同设备可能有不同的摄像头权限要求

在组件销毁时关闭摄像头,避免资源泄漏:

beforeDestroy() {
  if (this.codeReader) {
    this.codeReader.reset()
  }
}

处理扫描错误和权限问题:

methods: {
  onInit(promise) {
    promise.catch(error => {
      if (error.name === 'NotAllowedError') {
        alert('请允许摄像头访问权限')
      } else if (error.name === 'NotFoundError') {
        alert('未找到摄像头设备')
      } else {
        alert('摄像头初始化错误')
      }
    })
  }
}

标签: vue
分享给朋友:

相关文章

vue实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖 确…

vue实现ios

vue实现ios

Vue 实现 iOS 风格应用 使用 Vue 实现 iOS 风格的应用,可以通过结合 UI 框架和自定义样式来达成目标。以下是具体方法和步骤: 选择 iOS 风格的 UI 框架 Vue 生态中有多…

vue 实现fadeout

vue 实现fadeout

Vue 实现淡出效果 在 Vue 中实现淡出(fade-out)效果可以通过 CSS 过渡或动画结合 Vue 的动态渲染功能完成。以下是几种常见方法: 使用 Vue 过渡(Transition)组件…

vue 简单实现

vue 简单实现

以下是 Vue 的简单实现示例,涵盖基础模板、数据绑定、方法及生命周期: 基础模板与数据绑定 <div id="app"> <p>{{ message }}</p&…

vue实现抽屉

vue实现抽屉

Vue 实现抽屉组件 抽屉组件是一种常见的 UI 模式,通常用于从屏幕边缘滑出内容。以下是几种实现抽屉的方法: 使用 Vue 原生实现 创建一个基本的抽屉组件,利用 Vue 的过渡和条件渲染功能。…

vue怎样实现mvvm

vue怎样实现mvvm

Vue 实现 MVVM 的原理 Vue 的 MVVM(Model-View-ViewModel)模式通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 MVVM 的核心机制: 数…