当前位置:   article > 正文

基于html5QrCode实现的H5扫码功能(uni-app v2版本)_html5-qrcode.min.js

html5-qrcode.min.js

1. 安装(选择一种方式)

npm i html5-qrcode
  •   直接引入html5-qrcode.min.js文件 (本例使用此方法)

2. 扫码组件代码(先引入Html5Qrcode资源)

  1. <template>
  2. <view class="dialog-mask" v-if="value">
  3. <view class="dialog-box">
  4. <view id="qr-reader"></view>
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'Scan',
  11. model: { props: 'value', event: 'close' },
  12. props: {
  13. value: { type: Boolean, default: false },
  14. },
  15. watch: {
  16. value(val) {
  17. if (val) {
  18. this.$nextTick(() => {
  19. this.getCameras()
  20. })
  21. }
  22. },
  23. },
  24. data() {
  25. return {
  26. cameraId: '',
  27. html5QrCode: '',
  28. }
  29. },
  30. beforeDestroy() {
  31. this.stop()
  32. },
  33. methods: {
  34. getCameras() {
  35. uni.showLoading({ title: '相机启动中...', icon: 'none' })
  36. Html5Qrcode.getCameras()
  37. .then((devices) => {
  38. /**
  39. * devices 是对象数组
  40. * 例如:[ { id: "id", label: "label" }]
  41. */
  42. if (devices && devices.length) {
  43. if (devices.length > 1) {
  44. this.cameraId = devices[1].id
  45. } else {
  46. this.cameraId = devices[0].id
  47. }
  48. console.log(this.cameraId, 'cameraId')
  49. this.start()
  50. }
  51. })
  52. .catch((err) => {
  53. this.close()
  54. uni.showToast({ title: '启用相机失败', icon: 'none' })
  55. })
  56. },
  57. start() {
  58. this.html5QrCode = new Html5Qrcode('qr-reader')
  59. setTimeout(() => {
  60. uni.hideLoading()
  61. }, 1500)
  62. this.html5QrCode
  63. .start(
  64. this.cameraId, // 传入cameraId参数,这个参数在之前的步骤中已经获取到.
  65. {
  66. fps: 10, // 设置摄像头的帧率为10帧每秒
  67. qrbox: { width: 300, height: 300 }, // 设置需要扫描的QR码区域,这里设置的是300x300的区域
  68. aspectRatio: 1.777778, // 设置扫描结果的宽高比为1.777778,即宽高比为根号2,即等腰梯形的宽高比
  69. },
  70. (qrCodeMessage) => {
  71. // 当成功读取到QR码时,执行此回调函数
  72. if (qrCodeMessage) {
  73. this.qrCodeMessage = qrCodeMessage
  74. this.$emit('success', qrCodeMessage)
  75. this.close()
  76. this.stop()
  77. }
  78. },
  79. (errorMessage) => {},
  80. )
  81. .catch((err) => {
  82. // 如果扫描启动失败,执行此catch块中的代码
  83. uni.showToast({ title: `扫码失败:${err}`, icon: 'none' })
  84. })
  85. },
  86. stop() {
  87. this.html5QrCode &&
  88. this.html5QrCode.stop().finally(() => {
  89. this.html5QrCode.clear()
  90. this.html5QrCode = null
  91. })
  92. },
  93. close() {
  94. this.$emit('close', false)
  95. },
  96. },
  97. }
  98. </script>
  99. <style lang="scss" scoped>
  100. .dialog-mask {
  101. position: fixed;
  102. top: 0;
  103. left: 0;
  104. z-index: 99;
  105. height: 100vh;
  106. width: 100vw;
  107. background-color: rgba(0, 0, 0, 0.7);
  108. .dialog-box {
  109. position: absolute;
  110. left: 0;
  111. top: 0;
  112. width: 100vw;
  113. height: 100vh;
  114. display: flex;
  115. align-items: center;
  116. #qr-reader {
  117. width: 750rpx;
  118. }
  119. }
  120. }
  121. </style>

3. 扫描组件使用方法

  1. 引入组件
  2. 使用组件,通过v-model控制显示与隐藏,success回调获取扫码结果。
  1. <!-- 识别二维码 -->
  2. <Scan v-model="showScan" @success="getScan" />

4. 实现效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/833347
推荐阅读
相关标签
  

闽ICP备14008679号