sweep3.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <template>
  2. <div>
  3. <van-button @click="handleQrcode">扫一扫</van-button>
  4. <div id="qr-reader" style="width:500px"></div>
  5. <div id="qr-reader-results">
  6. {{ results }}
  7. </div>
  8. </div>
  9. </template>
  10. <script setup>
  11. import {Html5QrcodeScanner, Html5Qrcode, Html5QrcodeScanType} from "html5-qrcode";
  12. let results = ref(null)
  13. const state = reactive({
  14. html5QrCode: null,
  15. fileList: []
  16. })
  17. const handleQrcode = () => {
  18. Html5Qrcode.getCameras()
  19. .then(devices => {
  20. if (devices && devices.length) {
  21. // 当前环境下能识别出摄像头,并且摄像头的数据可能不止一个
  22. state.html5QrCode = new Html5Qrcode('qr-reader')// reader 放置扫码功能的元素ID
  23. alert('摄像头识别成功')
  24. state.html5QrCode.start(
  25. // environment后置摄像头 user前置摄像头
  26. {facingMode: 'environment'},
  27. {
  28. fps: 1, // 可选,每n秒帧扫描一次
  29. qrbox: {
  30. width: 250,
  31. height: 250
  32. } // 扫描的UI框
  33. },
  34. (decodedText, decodedResult) => {
  35. // 扫描结果
  36. results.value = {
  37. decodedText,
  38. decodedResult
  39. }
  40. }
  41. )
  42. }
  43. })
  44. .catch((e) => {
  45. // 摄像头无访问权限
  46. alert(e)
  47. })
  48. }
  49. </script>
  50. <style scoped lang="scss">
  51. </style>