当前位置:   article > 正文

vue3实现扫码识别(二维码&条形码)_vue3 扫码

vue3 扫码

1.安装@zxing/library

npm i @zxing/library

2.页面引入

import { BrowserMultiFormatReader } from '@zxing/library';

3.实现步骤

 3.1 显示摄像头的部分

  1. <div class='AboutView'>
  2. <!-- 镜头区域 -->
  3. <video ref="video" id="video" class="scan-video" autoplay></video>
  4. <div class="but_box">
  5. <div> <span>机器编号:</span><input type="text" v-model="number"></div>
  6. <div> <span>料 号:</span><input type="text" v-model="scanText"></div>
  7. <div><button @click="openScan">扫码</button><button @click="postScanData">提交</button></div>
  8. </div>
  9. </div>

3.2 js代码

  1. const codeReader = ref(null);
  2. const scanText = ref('')
  3. const number = ref('')
  4. // 初始化相机
  5. const openScan = ()=>{
  6. codeReader.value = new BrowserMultiFormatReader()
  7. codeReader.value.getVideoInputDevices().then(videoDevices=>{
  8. let firstDeviceId = videoDevices[videoDevices.length - 1].deviceId;
  9. if (videoDevices.length > 1) {
  10. // 一般通过判断摄像头列表项里的 label 字段,'camera2 0, facing back' 字符串含有 'back' 和 '0',大部分机型是这样,如果有些机型没有,那就还是默认获取最后一个
  11. firstDeviceId = videoDevices.find(el => { return el.label.indexOf('back') > -1 && el.label.indexOf('0') > -1 }) ?
  12. videoDevices.find(el => { return el.label.indexOf('back') > -1 && el.label.indexOf('0') > -1 }).deviceId :
  13. videoDevices[videoDevices.length - 1].deviceId;
  14. }
  15. decodeFromInputVideoFunc(firstDeviceId);
  16. }).catch(err=>{
  17. console.log(err);
  18. })
  19. }
  20. // 扫码
  21. const decodeFromInputVideoFunc = (firstDeviceId)=> { // 使用摄像头扫描
  22. codeReader.value.reset(); // 重置
  23. codeReader.value.decodeFromInputVideoDeviceContinuously(firstDeviceId, 'video', (result, err) => {
  24. if (result) {
  25. alert(result.text)
  26. console.log('扫码结果', result);
  27. scanText.value = result.text;
  28. if (scanText.value) {
  29. // 识别成功关闭摄像头
  30. codeReader.value.reset()
  31. codeReader.value.stopContinuousDecodeFromInputVideoDevice()
  32. }
  33. }
  34. });
  35. }

4.完整代码

  1. <template>
  2. <div class='AboutView'>
  3. <!-- 镜头区域 -->
  4. <video ref="video" id="video" class="scan-video" autoplay></video>
  5. <div class="but_box">
  6. <div> <span>机器编号:</span><input type="text" v-model="number"></div>
  7. <div> <span>料 号:</span><input type="text" v-model="scanText"></div>
  8. <div><button @click="openScan">扫码</button><button @click="postScanData">提交</button></div>
  9. </div>
  10. </div>
  11. </template>
  12. <script setup>
  13. import { onMounted, ref } from 'vue'
  14. import { BrowserMultiFormatReader } from '@zxing/library';
  15. // import axios from 'axios'
  16. import { transferData } from '../server/common'
  17. const codeReader = ref(null);
  18. const scanText = ref('')
  19. const number = ref('')
  20. // 初始化相机
  21. const openScan = ()=>{
  22. codeReader.value = new BrowserMultiFormatReader()
  23. codeReader.value.getVideoInputDevices().then(videoDevices=>{
  24. let firstDeviceId = videoDevices[videoDevices.length - 1].deviceId;
  25. if (videoDevices.length > 1) {
  26. // 一般通过判断摄像头列表项里的 label 字段,'camera2 0, facing back' 字符串含有 'back' 和 '0',大部分机型是这样,如果有些机型没有,那就还是默认获取最后一个
  27. firstDeviceId = videoDevices.find(el => { return el.label.indexOf('back') > -1 && el.label.indexOf('0') > -1 }) ?
  28. videoDevices.find(el => { return el.label.indexOf('back') > -1 && el.label.indexOf('0') > -1 }).deviceId :
  29. videoDevices[videoDevices.length - 1].deviceId;
  30. }
  31. decodeFromInputVideoFunc(firstDeviceId);
  32. }).catch(err=>{
  33. console.log(err);
  34. })
  35. }
  36. // 扫码
  37. const decodeFromInputVideoFunc = (firstDeviceId)=> { // 使用摄像头扫描
  38. codeReader.value.reset(); // 重置
  39. codeReader.value.decodeFromInputVideoDeviceContinuously(firstDeviceId, 'video', (result, err) => {
  40. if (result) {
  41. alert(result.text)
  42. console.log('扫码结果', result);
  43. scanText.value = result.text;
  44. if (scanText.value) {
  45. // 识别成功关闭摄像头
  46. codeReader.value.reset()
  47. codeReader.value.stopContinuousDecodeFromInputVideoDevice()
  48. }
  49. }
  50. });
  51. }
  52. // 提交扫码结果
  53. const postScanData = ()=>{
  54. const scanData = {
  55. partno:scanText.value,
  56. deviceid:Number(number.value),
  57. }
  58. transferData(scanData).then(res=>{
  59. alert(res)
  60. })
  61. }
  62. onMounted(()=>{
  63. })
  64. </script>
  65. <style scoped lang='scss'>
  66. .AboutView {
  67. }
  68. video{
  69. margin-top: 20%;
  70. width: 80%;
  71. }
  72. .but_box{
  73. position: fixed;
  74. bottom: 10%;
  75. left: 50%;
  76. transform: translateX(-50%);
  77. div{
  78. display: flex;
  79. margin: 0.125rem 0;
  80. justify-content: space-around;
  81. span{
  82. width: 5.625rem;
  83. text-align: right;
  84. }
  85. input{
  86. text-align: center;
  87. }
  88. button{
  89. width: 8.4375rem;
  90. height: 2.75rem;
  91. font-size: 1.5625rem;
  92. font-weight: 600;
  93. color: lavenderblush;
  94. background-color: rgba(0, 183, 255,0.8);
  95. border: none;
  96. border-radius: 0.5rem;
  97. }
  98. }
  99. }
  100. </style>

5.效果图

6.注意:调用浏览器摄像头需要在https环境下运行

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

闽ICP备14008679号