[web教学] H5项目实现扫描二维码功能 -- html5-qrcode

[复制链接]
查看1024 | 回复0 | 2023-8-23 12:10:14 | 显示全部楼层 |阅读模式 来自 中国北京
vue3中使用html5-qrcode实现扫描二维码功能。
   实现原理:调取手机摄像头扫一扫功能实现
注:调取手机摄像头必要再https协议下才可以,有的项目启动设置https:true可以实现启动的项目协议为https且可以访问,有的还是必要证书才能访问
  实现

1、下载html5-qrcode

  1. npm i html5-qrcode
复制代码
2、使用

  1. <template>
  2.     <div class="container" v-show="isShow">
  3.       <div id="reader"></div>
  4.     </div>
  5. </template>
  6. <script setup>
  7. import { reactive, onMounted, onUnmounted, ref } from 'vue'
  8. import { showToast  } from 'vant';
  9. import { Html5Qrcode } from 'html5-qrcode'
  10. let html5QrCode = ref(null)
  11. onMounted(() => {
  12.   getCameras()
  13. })
  14. const onSearchOrder = () => {
  15.     console.log('在运运单')
  16.     router.push('order')
  17. }
  18. onUnmounted(() => {
  19.     stop()
  20. })
  21. const getCameras = () => {
  22.     Html5Qrcode.getCameras()
  23.         .then((devices) => {
  24.             if (devices && devices.length) {
  25.                 isShow.value = true
  26.                 html5QrCode = new Html5Qrcode('reader')
  27.                 // start开始扫描
  28.                 start()
  29.             }
  30.         })
  31.         .catch((err) => {
  32.             // handle err
  33.             console.log('获取设备信息失败', err) // 获取设备信息失败
  34.             showToast('获取设备信息失败')
  35.         })
  36. }
  37. const start = () => {
  38.    
  39.     html5QrCode
  40.         .start(
  41.             {facingMode: "environment" },
  42.             {
  43.                 fps: 20, // 设置每秒多少帧
  44.                 qrbox: { width: 250, height: 250 } // 设置取景范围
  45.                 // scannable, rest shaded.
  46.             },
  47.             (decodedText, decodedResult) => {
  48.                     alert('扫码结果' + decodedText)
  49.             },
  50.             (errorMessage) => {
  51.                 // parse error, ideally ignore it. For example:
  52.                 // console.log(`QR Code no longer in front of camera.`);
  53.                 console.log('暂无额扫描结果', errorMessage)
  54.             }
  55.         )
  56.         .catch((err) => {
  57.             // Start failed, handle it. For example,
  58.             console.log(`Unable to start scanning, error: ${err}`)
  59.         })
  60. }
  61. const stop = () => {
  62.     if (devicesInfo.value) {
  63.         html5QrCode
  64.             .stop()
  65.             .then((ignore) => {
  66.                 // QR Code scanning is stopped.
  67.                 console.log('QR Code scanning stopped.', ignore)
  68.             })
  69.             .catch((err) => {
  70.                 // Stop failed, handle it.
  71.                 console.log('Unable to stop scanning.', err)
  72.             })
  73.     }
  74. }
  75. </script>
  76. <style lang="scss" scoped>
  77. .container {
  78.     position: relative;
  79.     top: 0px;
  80.     left: 0px;
  81.     height: 100vh;
  82.     width: 100%;
  83.     background: rgba($color: #000000, $alpha: 0.48);
  84.     z-index: 999;
  85. }
  86. #reader {
  87.     top: 50%;
  88.     left: 0;
  89.     transform: translateY(-50%);
  90. }
  91. </style>
复制代码


来源:https://blog.csdn.net/weixin_44801790/article/details/129865050
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则