Vue移動端實(shí)現(xiàn)調(diào)用相機(jī)掃描二維碼或條形碼的全過程
一、開發(fā)前的準(zhǔn)備
實(shí)現(xiàn)二維碼或條形碼的掃描識別比較普遍的做法是去調(diào)用微信 JS-SDK 的掃一掃功能(詳見 概述 | 微信開放文檔),或者支付寶 H5 開放的API(詳見 支付寶H5開放文檔)。
但是這兩者都會比較麻煩且有一定的局限性,微信的掃一掃只能在微信里用,而且還需要公眾號認(rèn)證等配置操作。支付寶在內(nèi)置 App 內(nèi)可以同時識別二維碼和條形碼,但外部調(diào)用的 API 無法一次性同時識別,只能分開識別。
我這里就提供一個直接使用的開源庫:https://github.com/zxing-js/library,本人移動端前端開發(fā)的框架是 Vue,組件庫用的是 Vant,本文方案只要開發(fā)時用的電腦具有攝像頭就可以實(shí)現(xiàn)效果預(yù)覽。
二、實(shí)現(xiàn)效果圖
這里分享兩個在線工具
2、草料二維碼生成器 或者 點(diǎn)擊這里




可以看到這樣操作不用經(jīng)過任何打包(有的需要打包成 app 才行)、部署(有的需要部署到 https 的服務(wù)器才行)、配置(前面說的諸如微信開發(fā)的配置等...)。
三、具體操作實(shí)現(xiàn)
1、安裝。
npm install @zxing/library --save
2、假設(shè)場景:頁面上有個按鈕,點(diǎn)擊觸發(fā)掃碼功能 @click='scanCode()',在 methods 寫入該方法。
scanCode() {
console.log('瀏覽器信息', navigator.userAgent);
this.$router.push({
path: '/scanCodePage'
});
}同時在 vue-router 寫入對應(yīng)頁面的路由。
{
title: '掃碼頁面',
name: 'scanCodePage',
path: '/scanCodePage',
component: () => import('@/views/scanCodePage.vue')
}3、掃碼頁面代碼,通過與 video 標(biāo)簽結(jié)合使用,把以下代碼直接全部拷貝到新建的一個 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>
<!-- 提示語 -->
<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 = '正在調(diào)用攝像頭...';
console.log('videoInputDevices', videoInputDevices);
// 默認(rèn)獲取第一個攝像頭設(shè)備id
let firstDeviceId = videoInputDevices[0].deviceId;
// 獲取第一個攝像頭設(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)獲取第一個攝像頭設(shè)備id
let firstDeviceId = videoInputDevices[0].deviceId;
// 獲取第一個攝像頭設(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 = '正在嘗試識別...';
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 = '識別失敗';
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>總結(jié)
到此這篇關(guān)于Vue移動端實(shí)現(xiàn)調(diào)用相機(jī)掃描二維碼或條形碼的文章就介紹到這了,更多相關(guān)Vue調(diào)用相機(jī)掃描二維碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE+ElementUI下載文件的幾種方式(小結(jié))
本文主要介紹了VUE+ElementUI下載文件的幾種方式(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
快速解決Error: error:0308010C:digital envelope ro
因?yàn)?nbsp;node.js V17版本中最近發(fā)布的OpenSSL3.0, 而OpenSSL3.0對允許算法和密鑰大小增加了嚴(yán)格的限制,下面通過本文給大家分享快速解決Error: error:0308010C:digital envelope routines::unsupported的三種解決方案,感興趣的朋友一起看看吧2024-02-02
Vue 動態(tài)生成數(shù)據(jù)字段的實(shí)例
這篇文章主要介紹了Vue 動態(tài)生成數(shù)據(jù)字段的實(shí)例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
vue項(xiàng)目網(wǎng)頁自適應(yīng)等比例放大縮小實(shí)例代碼
等比例縮放可以在不同的分辨率下都能夠一屏展示,不會有滾動條的問題,也不會有適配問題,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目網(wǎng)頁自適應(yīng)等比例放大縮小的相關(guān)資料,需要的朋友可以參考下2022-11-11
一文詳解WebStorm如何調(diào)試Vue項(xiàng)目
這篇文章主要介紹了如何使用WebStorm進(jìn)行斷點(diǎn)調(diào)試,包括配置、啟動本地應(yīng)用程序、設(shè)置斷點(diǎn)以及使用調(diào)試工具等步驟,文中通過圖文及代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02
Vue3使用transition組件改變DOM屬性的方式小結(jié)
這篇文章主要為大家詳細(xì)介紹了Vue3中使用transition組件改變DOM屬性的常用方式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
vue-router如何實(shí)現(xiàn)history模式配置
這篇文章主要介紹了vue-router如何實(shí)現(xiàn)history模式配置,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
vue?路由跳轉(zhuǎn)打開新窗口被瀏覽器攔截問題處理
這篇文章主要介紹了vue?路由跳轉(zhuǎn)打開新窗口被瀏覽器攔截問題處理,下面文章操作中所遇到相關(guān)問題解決的內(nèi)容介紹詳細(xì),需要的小伙伴可以參考一下2022-03-03

