html5-qrcode前端打开摄像头扫描二维码功能

[复制链接]
发表于 2025-10-10 03:22:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

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

×

实现的效果如图所示,全屏打开而且扫描到二维码后弹窗提示,紧张就是使用html5-qrcode这个依赖库,html5-qrcode开源地点:GitHub - mebjas/html5-qrcode: A cross platform HTML5 QR code reader. See end to end implementation at: https://scanapp.org
使用文档:Getting started | ScanApp
安装依赖:
  1. pnpm install --save-dev html5-qrcode
复制代码
弹窗提示我用的vant这个ui库,开源地点:GitHub - youzan/vant: A lightweight, customizable Vue UI library for mobile web apps.
然后在项目中使用,我使用的是vue:
  1. <template>
  2.     <div class="scanCode">
  3.         <div id="reader"></div>
  4.     </div>
  5. </template>
  6. <script setup lang="ts">
  7. import { ref } from 'vue'
  8. import { showToast } from 'vant'
  9. import { Html5Qrcode } from 'html5-qrcode'
  10. const codeContent = ref('1024')
  11. // This method will trigger user permissions
  12. Html5Qrcode.getCameras()
  13.     .then((devices) => {
  14.         // console.log(devices)
  15.         const html5QrCode = new Html5Qrcode('reader')
  16.         const width = window.innerWidth
  17.         const height = window.innerHeight
  18.         const aspectRatio = width / height
  19.         const reverseAspectRatio = height / width
  20.         const mobileAspectRatio =
  21.             reverseAspectRatio > 1.5
  22.                 ? reverseAspectRatio + (reverseAspectRatio * 12) / 100
  23.                 : reverseAspectRatio
  24.         if (devices && devices.length) {
  25.             // .. use this to start scanning.
  26.             html5QrCode
  27.                 .start(
  28.                     { facingMode: { exact: 'environment' } },
  29.                     {
  30.                         fps: 10, // Optional, frame per seconds for qr code scanning
  31.                         aspectRatio: aspectRatio + 1,
  32.                         qrbox: { width: 250, height: 250 }, // Optional, if you want bounded box UI
  33.                         videoConstraints: {
  34.                             facingMode: 'environment',
  35.                             aspectRatio:
  36.                                 width < 600 ? mobileAspectRatio : aspectRatio,
  37.                         },
  38.                     },
  39.                     (decodedText, decodedResult) => {
  40.                         // do something when code is read
  41.                         console.log('decodedText', decodedText)
  42.                         // console.log('decodedResult', decodedResult)
  43.                         codeContent.value = decodedText
  44.                         showToast(decodedText)
  45.                     },
  46.                     (errorMessage) => {
  47.                         // parse error, ignore it.
  48.                         // console.log('parse error, ignore it.', errorMessage)
  49.                     }
  50.                 )
  51.                 .catch((err) => {
  52.                     // Start failed, handle it.
  53.                     console.log('Start failed, handle it.')
  54.                 })
  55.         }
  56.     })
  57.     .catch((err) => {
  58.         // handle err
  59.         console.log(err)
  60.     })
  61. </script>
  62. <style lang="scss" scoped>
  63. .scanCode {
  64.     width: 100%;
  65.     height: 100vh;
  66.     display: flex;
  67.     flex-direction: column;
  68.     background: rgba(0, 0, 0);
  69.     #reader {
  70.         top: 50%;
  71.         left: 0;
  72.         transform: translateY(-50%);
  73.     }
  74. }
  75. </style>
复制代码

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表