当前位置:   article > 正文

ZXing库实现扫描二维码_js扫描二维码插件 zxing

js扫描二维码插件 zxing

为什么要使用ZXing

因为在用uniapp的scan时扫描二维码在h5端不适用,因此h5端需要自己引入三方库,我这里用的是zxing完整代码在最后

一、引入ZXing

有两种引入方法,npm引入和js文件直接引入

npm add @zxing/library
import { BrowserMultiFormatReader } from '@zxing/library'

 js引入

地址:https://unpkg.com/@zxing/library@latest/umd/index.min.js

import { BrowserMultiFormatReader } from '@/utils/zxing.js'

二、功能实现

直接使用ZXing提供的方法就行,如果需要更改视频清晰度等等,请参考官方文档

1.获取摄像头

  1. // 这里的处理是因为uniapp会对video标签进行二次封装,vue的话直接用ref获取dom就行
  2. var video = document.getElementById('video_nav_id').getElementsByTagName('video')[0]
  3. video.setAttribute('id', 'video_id')
  4. video.setAttribute('class', 'video_calss')
  5. codeReader.value = new BrowserMultiFormatReader();
  6. codeReader.value.listVideoInputDevices().then(res => {
  7. if (res.length > 0) {
  8. videoInputDevices.value = res
  9. // 这里默认选择最后一个摄像头
  10. deviceId.value = res[res.length - 1].deviceId
  11. } else {
  12. // 没有可用的摄像头
  13. // todo
  14. }
  15. }).catch(err => {
  16. console.error(err)
  17. })

2.扫描

  1. try {
  2. codeReader.value.decodeFromVideoDevice(deviceId.value, 'video_id', (res, err) => {
  3. if (res) handleScanningResult(res)
  4. })
  5. } catch (err) {
  6. uni.showToast({ title: `初始化失败${err}`, icon: 'none' });
  7. }

3.完整代码

  1. <template>
  2. <view class="camera_page">
  3. <view class="camera_content">
  4. <view class="code_close" @click="closeClick()">
  5. <!-- <img src="../../static/close.png"> -->
  6. </view>
  7. <view class="loop_line"></view>
  8. <view class="video_nav">
  9. <video id="video_nav_id" autoplay :controls="false"></video>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script setup>
  15. import { onLoad } from "@dcloudio/uni-app";
  16. import { ref } from "vue";
  17. import { BrowserMultiFormatReader } from '@zxing/library' //npm引入
  18. // import { BrowserMultiFormatReader } from '@/utils/zxing.js' //直接引入js文件
  19. const codeReader = ref(null)
  20. const deviceId = ref(null)
  21. const videoInputDevices = ref([])
  22. const initEvent = () => {
  23. // 这里的处理是因为uniapp会对video标签进行二次封装,如果是Vue的话直接获取video标签
  24. var video = document.getElementById('video_nav_id').getElementsByTagName('video')[0] // uniapp
  25. // var video = document.getElementById('video_nav_id') //Vue
  26. video.setAttribute('id', 'video_id')
  27. video.setAttribute('class', 'video_calss')
  28. codeReader.value = new BrowserMultiFormatReader();
  29. codeReader.value.listVideoInputDevices().then(res => {
  30. if (res.length > 0) {
  31. videoInputDevices.value = res
  32. // 这里默认选择最后一个摄像头
  33. deviceId.value = res[res.length - 1].deviceId
  34. } else {
  35. // 没有可用的摄像头
  36. // todo
  37. }
  38. }).catch(err => {
  39. console.error(err)
  40. })
  41. startScanning()
  42. }
  43. onLoad(() => {
  44. setTimeout(() => {
  45. initEvent();
  46. }, 3000);
  47. });
  48. const startScanning = () => {
  49. try {
  50. codeReader.value.decodeFromVideoDevice(deviceId.value, 'video_id', (res, err) => {
  51. if (res) handleScanningResult(res)
  52. })
  53. } catch (err) {
  54. uni.showToast({ title: `初始化失败${err}`, icon: 'none' });
  55. }
  56. }
  57. const handleScanningResult = (res) => {
  58. // 这里处理扫描结果
  59. console.log(res.text)
  60. }
  61. // 停止扫描
  62. const stopScanning = () => {
  63. codeReader.value.reset()
  64. }
  65. const closeClick = () => {
  66. // todo
  67. }
  68. </script>
  69. <style scoped lang="scss">
  70. .camera_page {
  71. height: 100vh;
  72. width: 100vw;
  73. }
  74. .camera_content {
  75. height: 100%;
  76. width: 100%;
  77. position: relative;
  78. }
  79. .code_close {
  80. height: 50rpx;
  81. width: 50rpx;
  82. position: absolute;
  83. left: 30rpx;
  84. top: 30rpx;
  85. z-index: 999999;
  86. }
  87. .code_close>img {
  88. height: 100%;
  89. width: 100%;
  90. display: block;
  91. }
  92. .loop_line {
  93. height: 3px;
  94. width: 80%;
  95. background-color: aliceblue;
  96. border-radius: 50%;
  97. box-shadow: 1px -4px 25px 7px rgba(255, 255, 255, 0.5);
  98. position: absolute;
  99. left: 50%;
  100. top: 20%;
  101. transform: translateX(-50%);
  102. animation: myfirst 3s infinite;
  103. z-index: 999999;
  104. }
  105. @keyframes myfirst {
  106. from {
  107. top: 20%;
  108. }
  109. to {
  110. top: 80%;
  111. }
  112. }
  113. .video_nav {
  114. height: 100%;
  115. width: 100%;
  116. }
  117. #video_nav_id {
  118. height: 100%;
  119. width: 100%;
  120. }
  121. :deep(.uni-video-cover) {
  122. display: none;
  123. }
  124. </style>

注意

摄像头权限需要https协议才能调用

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

闽ICP备14008679号