• H5识别二维码返回二维码信息


    template

    <div class="qr-code qr-btn" @click="qrCodeSearch">
      <i class="iconfont icon-weibiaoti--2"></i>
    </div>

    methods

    qrCodeSearch() {
      this.$router.push(
        {
          name: 'scan'
        }
      )

    }

    scan.vue 

    <template>
      <div class="page-scan" :style="{height: wrapHeight + 'px'}">
        <div class="back" @click="goBack">
          <img class="border-img" 
            src="../../../../../assets/images/header/back.png" 
            lazy-load="true"
            />
        </div>
        <!-- 扫码区域 -->
        <video ref="video" id="video" class="scan-video" autoplay></video>
        <!-- 提示语 -->
        <div v-show="tipShow" class="scan-tip"> {{tipMsg}} </div>
      </div>
    </template>
    <script>
    import { BrowserMultiFormatReader } from '@zxing/library';
      export default {
        name: 'scanCodePage',
        data() {
          return {
            wrapHeight: null,
            loadingShow: false,
            codeReader: null,
            scanText: '',
            vin: null,
            tipMsg: '正在尝试识别....',
            tipShow: false
          }
        },
        created() {
          this.codeReader = new BrowserMultiFormatReader();
          this.openScan();
        },
        destroyed(){
          this.codeReader.reset();
        },
        watch: {
          '$route'(to, from) {
            if(to.path == '/scanCodePage'){
              this.codeReader = new BrowserMultiFormatReader();
              this.openScanTwo();
            }
          }
        },
        methods: {
          // 返回
          goBack() {
            this.$destroy();
            this.$router.back();
          },
          async openScan() {
            this.codeReader.getVideoInputDevices().then((videoInputDevices) => {
              this.tipShow = true;
              this.tipMsg = '正在调用摄像头...';
              console.log('videoInputDevices', videoInputDevices);
              // 默认获取第一个摄像头设备id
              let firstDeviceId = videoInputDevices[0].deviceId;
              // 获取第一个摄像头设备的名称
              const videoInputDeviceslablestr = JSON.stringify(videoInputDevices[0].label);
              if (videoInputDevices.length > 1) {
                // 判断是否后置摄像头
                if (videoInputDeviceslablestr.indexOf('back') > -1) {
                  firstDeviceId = videoInputDevices[0].deviceId;
                } else {
                  firstDeviceId = videoInputDevices[1].deviceId;
                }
              }
              this.decodeFromInputVideoFunc(firstDeviceId);
            }).catch(err => {
              this.tipShow = false;
              this.codeReader = null;
              this.$destroy();
           // 这部分接下去的代码根据需要,读者自行编写了
    this.$router.push({
                path: '/couponCancell',
                query: {
                  couConsumerCode: ''
                }
              })
              console.error(err);
            });
          },
          async openScanTwo() {
            this.codeReader = await new BrowserMultiFormatReader();
            this.codeReader.getVideoInputDevices().then((videoInputDevices) => {
              this.tipShow = true;
              this.tipMsg = '正在调用摄像头...';
              console.log('videoInputDevices', videoInputDevices);
              // 默认获取第一个摄像头设备id
              let firstDeviceId = videoInputDevices[0].deviceId;
              // 获取第一个摄像头设备的名称
              const videoInputDeviceslablestr = JSON.stringify(videoInputDevices[0].label);
              if (videoInputDevices.length > 1) {
                // 判断是否后置摄像头
                if (videoInputDeviceslablestr.indexOf('back') > -1) {
                  firstDeviceId = videoInputDevices[0].deviceId;
                } else {
                  firstDeviceId = videoInputDevices[1].deviceId;
                }
              }
              this.decodeFromInputVideoFunc(firstDeviceId);
            }).catch(err => {
              this.tipShow = false;
              this.$destroy();
          // 下面代码根据业务需求自行编写
              this.$router.push({
                path: '/couponCancell',
                query: {
                  couConsumerCode: ''
                }
              })
    
              console.error(err);
            });
          },
          decodeFromInputVideoFunc(firstDeviceId) {
            this.codeReader.reset(); // 重置
            this.scanText = '';
            this.codeReader.decodeFromInputVideoDeviceContinuously(firstDeviceId, 'video', (result, err) => {
              this.tipMsg = '正在尝试识别...';
              this.scanText = '';
              if (result) {
                console.log('扫描结果', result);
                this.scanText = result.text;
                if (this.scanText) {
                  this.tipShow = false;
                  this.$destroy();
            // 下面代码根据业务需求自行编写
                  this.$router.push({
                    path: '/couponCancell',
                    query: {
                      couConsumerCode: this.scanText
                    }
                  })
                }
              }
              if (err && !(err)) {
                this.tipMsg = '识别失败';
                setTimeout(() => {
                  this.tipShow = false;
                  this.$destroy();
            //   下面代码根据业务需求自行编写
                  this.$router.push({
                    path: '/couponCancell',
                    query: {
                      couConsumerCode: ''
                    }
                  })
                }, 2000)
                console.error(err);
              }
            });
          }
        },
        mounted() {
          this.wrapHeight = document.body.clientHeight;
        }
      }
    </script>
     
    <style lang="less" scoped>
    .page-scan .back{
      position:absolute;
      top:10;
      left:10px;
      height:30px;
      z-index: 100;
    }
    .page-scan .back img{
      height: 100%;
    }
    .scan-index-bar{
      background-image: linear-gradient( -45deg, #42a5ff ,#59cfff);
    }
    .van-nav-bar__title{
      color: #fff !important;
    }
    .scan-video{
      height: 90vh;
    }
    .scan-tip{
       100vw;
      text-align: center;
      margin-bottom: 10vh;
      color: white;
      font-size: 5vw;
    }
    .page-scan{
      overflow: hidden;
      background-color: #363636;
    }
    </style>
  • 相关阅读:
    Windows 2003/2008更改远程桌面端口脚本
    如何修改远程桌面连接3389端口
    关于百度地图(离线)使用过程报“Cannot read property 'jb' of undefined ”错误的解决办法
    IIS 错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
    IIS 错误:由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
    IIS7错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的(overrideModeDefault="Deny")......
    Jqgrid pager 关于“local” dataType 动态加载数据分页的研究(没好用的研究结果)
    JQGrid导出Excel文件
    Oracle以15分钟为界,统计一天内各时间段的数据笔数
    ORA-01438: 值大于为此列指定的允许精度
  • 原文地址:https://www.cnblogs.com/chenwan1218/p/15798360.html
Copyright © 2020-2023  润新知