html5項目實現(xiàn)掃描二維碼功能
發(fā)布時間:2023-10-07 16:25:11 作者:浮橋
我要評論
掃描二維碼是很常見的一種功能,本文就來介紹一下html5項目實現(xiàn)掃描二維碼功能,具有一定的參考價值,感興趣的可以了解一下
實現(xiàn)原理:調(diào)取手機(jī)攝像頭掃一掃功能實現(xiàn)
注:調(diào)取手機(jī)攝像頭需要再https協(xié)議下才可以,有的項目啟動配置https:true可以實現(xiàn)啟動的項目協(xié)議為https且可以訪問,有的還是需要證書才能訪問
實現(xiàn)
1、下載html5-qrcode
npm i html5-qrcode
2、使用
<template>
<div class="container" v-show="isShow">
<div id="reader"></div>
</div>
</template>
<script setup>
import { reactive, onMounted, onUnmounted, ref } from 'vue'
import { showToast } from 'vant';
import { Html5Qrcode } from 'html5-qrcode'
let html5QrCode = ref(null)
onMounted(() => {
getCameras()
})
const onSearchOrder = () => {
console.log('在運(yùn)運(yùn)單')
router.push('order')
}
onUnmounted(() => {
stop()
})
const getCameras = () => {
Html5Qrcode.getCameras()
.then((devices) => {
if (devices && devices.length) {
isShow.value = true
html5QrCode = new Html5Qrcode('reader')
// start開始掃描
start()
}
})
.catch((err) => {
// handle err
console.log('獲取設(shè)備信息失敗', err) // 獲取設(shè)備信息失敗
showToast('獲取設(shè)備信息失敗')
})
}
const start = () => {
html5QrCode
.start(
{facingMode: "environment" },
{
fps: 20, // 設(shè)置每秒多少幀
qrbox: { width: 250, height: 250 } // 設(shè)置取景范圍
// scannable, rest shaded.
},
(decodedText, decodedResult) => {
alert('掃碼結(jié)果' + decodedText)
},
(errorMessage) => {
// parse error, ideally ignore it. For example:
// console.log(`QR Code no longer in front of camera.`);
console.log('暫無額掃描結(jié)果', errorMessage)
}
)
.catch((err) => {
// Start failed, handle it. For example,
console.log(`Unable to start scanning, error: ${err}`)
})
}
const stop = () => {
if (devicesInfo.value) {
html5QrCode
.stop()
.then((ignore) => {
// QR Code scanning is stopped.
console.log('QR Code scanning stopped.', ignore)
})
.catch((err) => {
// Stop failed, handle it.
console.log('Unable to stop scanning.', err)
})
}
}
</script>
<style lang="scss" scoped>
.container {
position: relative;
top: 0px;
left: 0px;
height: 100vh;
width: 100%;
background: rgba($color: #000000, $alpha: 0.48);
z-index: 999;
}
#reader {
top: 50%;
left: 0;
transform: translateY(-50%);
}
</style>
到此這篇關(guān)于html5項目實現(xiàn)掃描二維碼功能的文章就介紹到這了,更多相關(guān)html5掃描二維碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章

基于MUI框架使用HTML5實現(xiàn)的二維碼掃描功能
這篇文章主要介紹了基于MUI框架使用HTML5實現(xiàn)的二維碼掃描功能,需要的朋友可以參考下2018-03-01HTML5實現(xiàn)分享到微信好友朋友圈QQ好友QQ空間微博二維碼功能
這篇文章主要介紹了HTML5實現(xiàn)分享到微信好友朋友圈QQ好友QQ空間微博二維碼功能的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-01-03
HTML5混合開發(fā)二維碼掃描以及調(diào)用本地攝像頭
今天主管給了我個需求,說要用混合開發(fā),用H5調(diào)用本地攝像頭進(jìn)行掃描二維碼,我之前有做過原生安卓的二維碼掃一掃,主要是通過調(diào)用zxing插件進(jìn)行操作的2017-12-27- 這篇文章主要介紹了Html5實現(xiàn)二維碼掃描并解析 的相關(guān)資料,需要的朋友可以參考下2016-01-20



