這篇文章主要介紹了Vue移動端怎么實現調用相機掃描二維碼或條形碼的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Vue移動端怎么實現調用相機掃描二維碼或條形碼文章都會有所收獲,下面我們一起來看看吧。
1、安裝。
npm install @zxing/library --save
2、假設場景:頁面上有個按鈕,點擊觸發掃碼功能 @click='scanCode()',在 methods 寫入該方法。
scanCode() { console.log('瀏覽器信息', navigator.userAgent); this.$router.push({ path: '/scanCodePage' }); }
同時在 vue-router 寫入對應頁面的路由。
{ title: '掃碼頁面', name: 'scanCodePage', path: '/scanCodePage', component: () => import('@/views/scanCodePage.vue') }
3、掃碼頁面代碼,通過與 video 標簽結合使用,把以下代碼直接全部拷貝到新建的一個 scanCodePage.vue 文件里使用,讀者在注釋的地方自行根據需求,編寫后續的業務代碼即可。
<template> <div class="page-scan"> <!--返回--> <van-nav-bar title="掃描二維碼/條形碼" fixed @click-left="clickIndexLeft()" class="scan-index-bar"> <template #left> <van-icon name="arrow-left" size="18" color="#fff"/> <span > 取消 </span> </template> </van-nav-bar> <!-- 掃碼區域 --> <video ref="video" id="video" class="scan-video" autoplay></video> <!-- 提示語 --> <div v-show="tipShow" class="scan-tip"> {{tipMsg}} </div> </div> </template> <script> import { BrowserMultiFormatReader } from '@zxing/library'; import { Dialog, Notify } from 'vant'; export default { name: 'scanCodePage', data() { return { loadingShow: false, codeReader: null, scanText: '', vin: null, tipMsg: '正在嘗試識別....', tipShow: false } }, created() { this.codeReader = new BrowserMultiFormatReader(); this.openScan(); }, destroyed(){ this.codeReader.reset(); }, watch: { '$route'(to, from) { if(to.path == '/scanCodePage'){ this.codeReader = new BrowserMultiFormatReader(); this.openScanTwo(); } } }, methods: { async openScan() { this.codeReader.getVideoInputDevices().then((videoInputDevices) => { this.tipShow = true; this.tipMsg = '正在調用攝像頭...'; console.log('videoInputDevices', videoInputDevices); // 默認獲取第一個攝像頭設備id let firstDeviceId = videoInputDevices[0].deviceId; // 獲取第一個攝像頭設備的名稱 const videoInputDeviceslablestr = JSON.stringify(videoInputDevices[0].label); if (videoInputDevices.length > 1) { // 判斷是否后置攝像頭 if (videoInputDeviceslablestr.indexOf('back') > -1) { firstDeviceId = videoInputDevices[0].deviceId; } else { firstDeviceId = videoInputDevices[1].deviceId; } } this.decodeFromInputVideoFunc(firstDeviceId); }).catch(err => { this.tipShow = false; console.error(err); }); }, async openScanTwo() { this.codeReader = await new BrowserMultiFormatReader(); this.codeReader.getVideoInputDevices().then((videoInputDevices) => { this.tipShow = true; this.tipMsg = '正在調用攝像頭...'; console.log('videoInputDevices', videoInputDevices); // 默認獲取第一個攝像頭設備id let firstDeviceId = videoInputDevices[0].deviceId; // 獲取第一個攝像頭設備的名稱 const videoInputDeviceslablestr = JSON.stringify(videoInputDevices[0].label); if (videoInputDevices.length > 1) { // 判斷是否后置攝像頭 if (videoInputDeviceslablestr.indexOf('back') > -1) { firstDeviceId = videoInputDevices[0].deviceId; } else { firstDeviceId = videoInputDevices[1].deviceId; } } this.decodeFromInputVideoFunc(firstDeviceId); }).catch(err => { this.tipShow = false; console.error(err); }); }, decodeFromInputVideoFunc(firstDeviceId) { this.codeReader.reset(); // 重置 this.scanText = ''; this.codeReader.decodeFromInputVideoDeviceContinuously(firstDeviceId, 'video', (result, err) => { this.tipMsg = '正在嘗試識別...'; this.scanText = ''; if (result) { console.log('掃描結果', result); this.scanText = result.text; if (this.scanText) { this.tipShow = false; // 這部分接下去的代碼根據需要,讀者自行編寫了 // this.$store.commit('app/SET_SCANTEXT', result.text); // console.log('已掃描的小票列表', this.$store.getters.scanTextArr); } } if (err && !(err)) { this.tipMsg = '識別失敗'; setTimeout(() => { this.tipShow = false; }, 2000) console.error(err); } }); }, clickIndexLeft(){ // 返回上一頁 this.codeReader = null; this.$destroy(); this.$router.back(); } } } </script> <style lang="scss"> .scan-index-bar{ background-image: linear-gradient( -45deg, #42a5ff ,#59cfff); } .van-nav-bar__title{ color: #fff !important; } .scan-video{ height: 80vh; } .scan-tip{ width: 100vw; text-align: center; margin-bottom: 10vh; color: white; font-size: 5vw; } .page-scan{ overflow-y: hidden; background-color: #363636; } </style>
關于“Vue移動端怎么實現調用相機掃描二維碼或條形碼”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Vue移動端怎么實現調用相機掃描二維碼或條形碼”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。