Vue移動(dòng)端實(shí)現(xiàn)調(diào)用相機(jī)掃描二維碼或條形碼的全過(guò)程
一、開發(fā)前的準(zhǔn)備
實(shí)現(xiàn)二維碼或條形碼的掃描識(shí)別比較普遍的做法是去調(diào)用微信 JS-SDK 的掃一掃功能(詳見 概述 | 微信開放文檔),或者支付寶 H5 開放的API(詳見 支付寶H5開放文檔)。
但是這兩者都會(huì)比較麻煩且有一定的局限性,微信的掃一掃只能在微信里用,而且還需要公眾號(hào)認(rèn)證等配置操作。支付寶在內(nèi)置 App 內(nèi)可以同時(shí)識(shí)別二維碼和條形碼,但外部調(diào)用的 API 無(wú)法一次性同時(shí)識(shí)別,只能分開識(shí)別。
我這里就提供一個(gè)直接使用的開源庫(kù):https://github.com/zxing-js/library,本人移動(dòng)端前端開發(fā)的框架是 Vue,組件庫(kù)用的是 Vant,本文方案只要開發(fā)時(shí)用的電腦具有攝像頭就可以實(shí)現(xiàn)效果預(yù)覽。
二、實(shí)現(xiàn)效果圖
這里分享兩個(gè)在線工具
2、草料二維碼生成器 或者 點(diǎn)擊這里
可以看到這樣操作不用經(jīng)過(guò)任何打包(有的需要打包成 app 才行)、部署(有的需要部署到 https 的服務(wù)器才行)、配置(前面說(shuō)的諸如微信開發(fā)的配置等...)。
三、具體操作實(shí)現(xiàn)
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è)面代碼,通過(guò)與 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 style="color: #fff"> 取消 </span> </template> </van-nav-bar> <!-- 掃碼區(qū)域 --> <video ref="video" id="video" class="scan-video" autoplay></video> <!-- 提示語(yǔ) --> <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>
總結(jié)
到此這篇關(guān)于Vue移動(dòng)端實(shí)現(xiàn)調(diào)用相機(jī)掃描二維碼或條形碼的文章就介紹到這了,更多相關(guān)Vue調(diào)用相機(jī)掃描二維碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目中路徑使用@和~的區(qū)別及說(shuō)明
這篇文章主要介紹了vue項(xiàng)目中路徑使用@和~的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12vue2實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的代碼
這篇文章主要介紹了vue2實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的代碼,需要的朋友可以參考下2018-08-08Vue項(xiàng)目中接口調(diào)用的詳細(xì)講解
應(yīng)公司需求,接口需要對(duì)接vue,記錄一下碰到的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目中接口調(diào)用的詳細(xì)講解,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07ant design Vue 純前端實(shí)現(xiàn)分頁(yè)問(wèn)題
這篇文章主要介紹了ant design Vue 純前端實(shí)現(xiàn)分頁(yè)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04解決vue項(xiàng)目打包上服務(wù)器顯示404錯(cuò)誤,本地沒出錯(cuò)的問(wèn)題
這篇文章主要介紹了解決vue項(xiàng)目打包上服務(wù)器顯示404錯(cuò)誤,本地沒出錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11Vue3?Suspense實(shí)現(xiàn)優(yōu)雅處理異步數(shù)據(jù)加載
Suspense?是?Vue?3?中用于處理異步數(shù)據(jù)加載的特性,它使得在加載異步數(shù)據(jù)時(shí)可以提供更好的用戶體驗(yàn),下面小編就來(lái)和大家詳細(xì)講講Suspense如何優(yōu)雅處理異步數(shù)據(jù)加載吧2023-10-10Vue?export?default中的name屬性有哪些作用
這篇文章主要介紹了Vue?export?default中的name屬性有哪些作用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03vue修改數(shù)據(jù)視圖更新原理學(xué)習(xí)
這篇文章主要為大家介紹了vue修改數(shù)據(jù)視圖更新原理學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11