当前位置:   article > 正文

html5-qrcode实现扫码功能——2024-04-19

html5-qrcode

项目需求:H5项目需要实现扫描二维码或条形码功能。

html5-qrcode使用

1.安装

npm install html5-qrcode --save-dev 

2.引入

  1. import { Html5Qrcode } from 'html5-qrcode';

 3.定义所需变量

  1. data: function() {
  2. return {
  3. html5QrCode: null,
  4. isShow: false,
  5. scanReasonList: []
  6. };
  7. },

4.创建容器(可根据需求写样式)

  1. <div class="reader" v-if="isShow">
  2. <div id="reader-box"></div>
  3. </div>

5.实现扫码逻辑

在一个button或者其他节点上绑定事件“scanCode”

  1. scanCode() {
  2. this.startScan();
  3. },
  4. startScan() {
  5. this.isShow = true;
  6. Html5Qrcode.getCameras().then(devices => {
  7. if (devices && devices.length) {
  8. this.html5Qrcode = new Html5Qrcode('reader-box');
  9. this.html5Qrcode.start(
  10. {
  11. facingMode: 'environment'
  12. },
  13. {
  14. fps: 10,
  15. qrbox: { width: 250, height: 250 }
  16. },
  17. (decodeText, decodeResult) => {
  18. if (decodeText) {
  19. this.scanReasonList.push(decodeText);
  20. this.stopScan();
  21. this.isShow = false;
  22. }
  23. },
  24. err => {
  25. console.log('失败', err);
  26. }
  27. );
  28. }
  29. });
  30. },
  31. stopScan() {
  32. this.html5Qrcode.stop();
  33. },

至此,可基本实现扫码功能。具体的参数配置可参考官网 Getting started | ScanApp

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

闽ICP备14008679号