您好,登錄后才能下訂單哦!
這篇文章主要介紹了Vue移動(dòng)端怎么實(shí)現(xiàn)調(diào)用相機(jī)掃描二維碼或條形碼的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Vue移動(dòng)端怎么實(shí)現(xiàn)調(diào)用相機(jī)掃描二維碼或條形碼文章都會(huì)有所收獲,下面我們一起來看看吧。
1、安裝。
npm install @zxing/library --save
2、假設(shè)場(chǎng)景:頁(yè)面上有個(gè)按鈕,點(diǎn)擊觸發(fā)掃碼功能 @click='scanCode()',在 methods 寫入該方法。
scanCode() { console.log('瀏覽器信息', navigator.userAgent); this.$router.push({ path: '/scanCodePage' }); }
同時(shí)在 vue-router 寫入對(duì)應(yīng)頁(yè)面的路由。
{ title: '掃碼頁(yè)面', name: 'scanCodePage', path: '/scanCodePage', component: () => import('@/views/scanCodePage.vue') }
3、掃碼頁(yè)面代碼,通過與 video 標(biāo)簽結(jié)合使用,把以下代碼直接全部拷貝到新建的一個(gè) scanCodePage.vue 文件里使用,讀者在注釋的地方自行根據(jù)需求,編寫后續(xù)的業(yè)務(wù)代碼即可。
<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> <!-- 掃碼區(qū)域 --> <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: '正在嘗試識(shí)別....', 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 = '正在調(diào)用攝像頭...'; console.log('videoInputDevices', videoInputDevices); // 默認(rèn)獲取第一個(gè)攝像頭設(shè)備id let firstDeviceId = videoInputDevices[0].deviceId; // 獲取第一個(gè)攝像頭設(shè)備的名稱 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 = '正在調(diào)用攝像頭...'; console.log('videoInputDevices', videoInputDevices); // 默認(rèn)獲取第一個(gè)攝像頭設(shè)備id let firstDeviceId = videoInputDevices[0].deviceId; // 獲取第一個(gè)攝像頭設(shè)備的名稱 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 = '正在嘗試識(shí)別...'; this.scanText = ''; if (result) { console.log('掃描結(jié)果', result); this.scanText = result.text; if (this.scanText) { this.tipShow = false; // 這部分接下去的代碼根據(jù)需要,讀者自行編寫了 // this.$store.commit('app/SET_SCANTEXT', result.text); // console.log('已掃描的小票列表', this.$store.getters.scanTextArr); } } if (err && !(err)) { this.tipMsg = '識(shí)別失敗'; setTimeout(() => { this.tipShow = false; }, 2000) console.error(err); } }); }, clickIndexLeft(){ // 返回上一頁(yè) 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>
關(guān)于“Vue移動(dòng)端怎么實(shí)現(xiàn)調(diào)用相機(jī)掃描二維碼或條形碼”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Vue移動(dòng)端怎么實(shí)現(xiàn)調(diào)用相機(jī)掃描二維碼或條形碼”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。