使用的html5-qrcode库,兼容app中H5扫码,需要Andriod同事调试下打开相机权限,需要iOS同事调试
参考文章:这里进入
- npm install
npm install html5-qrcode
- 新建个qrcode.vue组件:
<template>
<div class="qrcode">
<div id="reader" />
</div>
</template>
<script>
import { Html5Qrcode } from 'html5-qrcode'
export default {
created() {
this.getCameras()
},
beforeDestroy() {
this.stop()
},
methods: {
getCameras() {
Html5Qrcode.getCameras().then((devices) => {
if (devices && devices.length) {
this.html5QrCode = new Html5Qrcode('reader')
this.start()
}
}).catch((err) => {
// handle err
console.log(err)
this.html5QrCode = new Html5Qrcode('reader')
this.error = 'ERROR: 您需要授予相机访问权限'
this.$emit('err', this.error)
})
},
start() {
// environment后置 user前置
this.html5QrCode.start(
{ facingMode: 'environment' },
{
fps: 2,
qrbox: { width: 250, height: 250 }
},
(decodedText, decodedResult) => {
this.$emit('ok', decodedText)
}
).catch((err) => {
this.$emit('err', err)
})
},
stop() {
this.html5QrCode.stop().then((ignore) => {
// QR Code scanning is stopped.
console.log('QR Code scanning stopped.')
}).catch((err) => {
// Stop failed, handle it.
console.log('Unable to stop scanning.', err)
})
}
}
}
</script>
<style lang="scss" scoped>
.qrcode{
position: relative;
height: 100%;
width: 100%;
background: rgba($color: #000000, $alpha: 0.48);
}
#reader{
top: 0%;
left: 0;
}
</style>
- 引用
<template>
<BarScan
ref="qrcode"
@ok="getResult"
@err="geterror"
/>
</template>
<script>
import BarScan from '@/components/BarScan.vue'
import Vconsole from 'vconsole' // 打印工具
export default {
components: {
BarScan
},
mounted() {
const vconsole = new Vconsole()
},
methods: {
getResult(result) {
this.result = result
console.log(4444, result)
},
geterror(e) {
console.log(5555, e)
} }
}
</script>
很赞同村上春树的一句话:
“有一个喜欢的人真的太重要了,在你打算稀里糊涂过这普普通通的一生的时候,会因为对方而再想努力一下,在那么多疲惫不堪甚至抬不起头的日子,会因为对方而觉得人生还有盼头。”正确的爱是良性的相互教育,是强有力的向上攀爬,是治愈彼此人生滚烫的阳光。