Vue?調(diào)用攝像頭掃描條碼功能實(shí)現(xiàn)代碼
以下是一個(gè)基于 Vue.js 的頁面代碼示例,用于調(diào)用攝像頭并掃描條碼。我們將使用 jsQR
庫來解析二維碼(或條形碼),這是一個(gè)輕量級的 JavaScript 庫。
實(shí)現(xiàn)步驟:
- 安裝依賴:需要引入
jsQR
庫。 - 調(diào)用攝像頭:通過
navigator.mediaDevices.getUserMedia
獲取攝像頭視頻流。 - 解析條碼:使用
jsQR
對視頻幀進(jìn)行解析。
代碼實(shí)現(xiàn)
1. 安裝依賴
在項(xiàng)目中安裝 jsQR
:
npm install jsqr
2. Vue 頁面代碼
以下是完整的 Vue 頁面代碼:
<template> <div class="scanner-container"> <h2>條碼掃描</h2> <video ref="video" autoplay playsinline></video> <canvas ref="canvas" style="display: none;"></canvas> <p v-if="barcode">掃描結(jié)果: {{ barcode }}</p> <button @click="startScanner" :disabled="isScanning">開始掃描</button> <button @click="stopScanner" :disabled="!isScanning">停止掃描</button> </div> </template> <script> import jsQR from "jsqr"; export default { data() { return { isScanning: false, barcode: null, videoStream: null, }; }, methods: { async startScanner() { try { // 請求訪問攝像頭 this.videoStream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" }, // 使用后置攝像頭 }); this.$refs.video.srcObject = this.videoStream; // 等待視頻元數(shù)據(jù)加載完成 await new Promise((resolve) => { this.$refs.video.onloadedmetadata = resolve; }); this.isScanning = true; this.scanBarcode(); // 開始掃描 } catch (error) { console.error("無法訪問攝像頭:", error); alert("無法訪問攝像頭,請檢查權(quán)限設(shè)置!"); } }, stopScanner() { if (this.videoStream) { const tracks = this.videoStream.getTracks(); tracks.forEach((track) => track.stop()); this.videoStream = null; this.isScanning = false; } }, scanBarcode() { if (!this.isScanning) return; const video = this.$refs.video; const canvas = this.$refs.canvas; const context = canvas.getContext("2d", { willReadFrequently: true }); // 添加此選項(xiàng) // 設(shè)置 canvas 尺寸與視頻一致 canvas.width = video.videoWidth; canvas.height = video.videoHeight; // 繪制視頻幀到 canvas context.drawImage(video, 0, 0, canvas.width, canvas.height); // 獲取圖像數(shù)據(jù) const imageData = context.getImageData(0, 0, canvas.width, canvas.height); // 使用 jsQR 解析條碼 const code = jsQR(imageData.data, imageData.width, imageData.height); if (code) { this.barcode = code.data; // 保存掃描結(jié)果 this.stopScanner(); // 停止掃描 } else { // 繼續(xù)掃描下一幀 requestAnimationFrame(this.scanBarcode); } }, }, beforeDestroy() { this.stopScanner(); // 離開頁面時(shí)停止攝像頭 }, }; </script> <style scoped> .scanner-container { text-align: center; margin-top: 20px; } video { width: 100%; max-width: 400px; margin: 20px auto; border: 1px solid #ccc; } button { margin: 10px; padding: 10px 20px; font-size: 16px; cursor: pointer; } </style>
功能說明
開始掃描:
- 點(diǎn)擊“開始掃描”按鈕后,頁面會請求訪問設(shè)備攝像頭,并顯示實(shí)時(shí)視頻流。
- 使用
jsQR
對每一幀視頻進(jìn)行解析,直到成功識別條碼。
停止掃描:
- 點(diǎn)擊“停止掃描”按鈕后,攝像頭會被關(guān)閉,掃描停止。
掃描結(jié)果顯示:
- 當(dāng)成功解析條碼后,掃描結(jié)果會顯示在頁面上,并自動停止掃描。
注意事項(xiàng)
瀏覽器兼容性:
- 需要在支持
getUserMedia
的現(xiàn)代瀏覽器中運(yùn)行(如 Chrome、Edge)。 HTTPS
環(huán)境下才能正常使用攝像頭。
權(quán)限問題:
- 用戶需授予攝像頭訪問權(quán)限,否則無法正常工作。
性能優(yōu)化:
- 如果掃描速度較慢,可以調(diào)整
canvas
的分辨率以提高性能。
到此這篇關(guān)于Vue 調(diào)用攝像頭掃描條碼的文章就介紹到這了,更多相關(guān)Vue 調(diào)用掃描條碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決vue單頁路由跳轉(zhuǎn)后scrollTop的問題
今天小編就為大家分享一篇解決vue單頁路由跳轉(zhuǎn)后scrollTop的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue3路由跳轉(zhuǎn)params傳參接收不到的解決辦法
這篇文章主要給大家介紹了關(guān)于vue3路由跳轉(zhuǎn)params傳參接收不到的解決辦法,Vue3是目前前端開發(fā)中非常流行的框架之一,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06Nuxt 嵌套路由nuxt-child組件用法(父子頁面組件的傳值)
這篇文章主要介紹了Nuxt 嵌套路由nuxt-child組件用法(父子頁面組件的傳值),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11關(guān)于vue3.0使用axios報(bào)錯(cuò)問題
這篇文章主要介紹了vue3.0使用axios報(bào)錯(cuò)問題記錄,vue-cli3.0安裝插件的時(shí)候要注意區(qū)分vue-cli2.0的命令,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08