html5qrcode的使用(自留笔记)

本次项目为基于vue2+vant的H5,套壳APP,无法使用APK的扫码时选择使用h5qrcode进行扫码,实现扫码单。

1.安装html5-qrcode

 npm i html5-qrcode
2.在所需组件处使用

模版:

 <div class="canTab" @click="startScan" v-if="!isScanning"><van-icon name="scan" size="45" color="#fff" /></div>    


<div class="scan" v-if="isScanning">
      <div class="scan-box">
        <div id="scanning" width="800px"></div>
      </div>
      <span class="close-scan" @click="stopScan">
          <svg t="1681382339822" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2626" width="200" height="200">
            <path d="M512 960c-247.039484 0-448-200.960516-448-448S264.960516 64 512 64 960 264.960516 960 512 759.039484 960 512 960zM512 128.287273c-211.584464 0-383.712727 172.128262-383.712727 383.712727 0 211.551781 172.128262 383.712727 383.712727 383.712727 211.551781 0 383.712727-172.159226 383.712727-383.712727C895.712727 300.415536 723.551781 128.287273 512 128.287273z" fill="#ffffff" p-id="2627"></path>
            <path d="M557.05545 513.376159l138.367639-136.864185c12.576374-12.416396 12.672705-32.671738 0.25631-45.248112s-32.704421-12.672705-45.248112-0.25631l-138.560301 137.024163-136.447897-136.864185c-12.512727-12.512727-32.735385-12.576374-45.248112-0.063647-12.512727 12.480043-12.54369 32.735385-0.063647 45.248112l136.255235 136.671523-137.376804 135.904314c-12.576374 12.447359-12.672705 32.671738-0.25631 45.248112 6.271845 6.335493 14.496116 9.504099 22.751351 9.504099 8.12794 0 16.25588-3.103239 22.496761-9.247789l137.567746-136.064292 138.687596 139.136568c6.240882 6.271845 14.432469 9.407768 22.65674 9.407768 8.191587 0 16.352211-3.135923 22.591372-9.34412 12.512727-12.480043 12.54369-32.704421 0.063647-45.248112L557.05545 513.376159z" fill="#ffffff" p-id="2628"></path>
          </svg>
        </span>
    </div>

JS:

  // 引入扫描二维码
  import { Html5Qrcode } from 'html5-qrcode';


   data() {
      return {
        isScanning: false, // 扫码
      }


method: {
   //扫码接单
      startScan(){
        this.isScanning = true
        this.$nextTick(() => {
          this.html5QrCode = new Html5Qrcode('scanning');
          this.html5QrCode.start({
              facingMode: "environment"
            }, {
              fps: 10,
              aspectRatio: 1, // 4:3:1.333334 16:9:1.777778 1:1:1.0
              qrbox: {
                width: 280,
                height: 280
              },
            }, (decodedText, decodedResult) => {
            let codeUrl = decodedText; //获取二维码信息
            // 以下为自己所需的业务逻辑处理
            
            
              this.stopScan() // 结束扫码
              // alert("识别结果" + decodedText)
            }, errorMessage => {
            },
          )
            .catch(err => {});
        });
      },
      // 结束扫码
      stopScan() {
        this.html5QrCode.stop().then(() => {
          this.isScanning = false;
        })
      },

}

style:

  /* 扫码样式 */
  .scan {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #000;

    .scan-box {
      width: 100%;
    }

    .close-scan {
      position: absolute;
      top: 20px;
      right: 20px;

      svg {
        width: 40px;
        height: 40px;
      }
    }
  }
  .start-scan{
    position: absolute;
    top: calc(50% - 25px);
    left: calc(50% - 100px);
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    width: 200px;
    height: 50px;
    background: #00aaff;
    color: #fff;
    border-radius: 10px;
  }

这是复制别人的逻辑,已经表明出处,仅供自己记录笔记,防止以后使用时忘记。

完整代码(此处仅有扫描二维码代码,其他已摘去):

<template>
  <div class="driverMyBox">
    <div class="canTab" @click="startScan" v-if="!isScanning">
        <van-icon name="scan" size="45" color="#fff" />
    </div>
    <div class="scan" v-if="isScanning">
      <div class="scan-box">
        <div id="scanning" width="800px"></div>
      </div>
      <span class="close-scan" @click="stopScan">
          <svg t="1681382339822" class="icon" viewBox="0 0 1024 1024" version="1.1"                 xmlns="http://www.w3.org/2000/svg" p-id="2626" width="200" height="200">
            <path d="M512 960c-247.039484 0-448-200.960516-448-448S264.960516 64 512 64 960 264.960516 960 512 759.039484 960 512 960zM512 128.287273c-211.584464 0-383.712727 172.128262-383.712727 383.712727 0 211.551781 172.128262 383.712727 383.712727 383.712727 211.551781 0 383.712727-172.159226 383.712727-383.712727C895.712727 300.415536 723.551781 128.287273 512 128.287273z" fill="#ffffff" p-id="2627"></path>
            <path d="M557.05545 513.376159l138.367639-136.864185c12.576374-12.416396 12.672705-32.671738 0.25631-45.248112s-32.704421-12.672705-45.248112-0.25631l-138.560301 137.024163-136.447897-136.864185c-12.512727-12.512727-32.735385-12.576374-45.248112-0.063647-12.512727 12.480043-12.54369 32.735385-0.063647 45.248112l136.255235 136.671523-137.376804 135.904314c-12.576374 12.447359-12.672705 32.671738-0.25631 45.248112 6.271845 6.335493 14.496116 9.504099 22.751351 9.504099 8.12794 0 16.25588-3.103239 22.496761-9.247789l137.567746-136.064292 138.687596 139.136568c6.240882 6.271845 14.432469 9.407768 22.65674 9.407768 8.191587 0 16.352211-3.135923 22.591372-9.34412 12.512727-12.480043 12.54369-32.704421 0.063647-45.248112L557.05545 513.376159z" fill="#ffffff" p-id="2628"></path>
          </svg>
       </span>
    </div>
  </div>
</template>

<script type='text/javascript'>

 // 引入扫描二维码
  import { Html5Qrcode } from 'html5-qrcode';
  export default {
    data() {
      return {
        //扫码
        isScanning: false, // 是否正在扫一扫
      }
    //扫码接单
    startScan(){
        this.isScanning = true;
        this.$nextTick(() => {
          this.html5QrCode = new Html5Qrcode('scanning');
          this.html5QrCode.start({
              facingMode: "environment"
            }, {
              fps: 10,
              aspectRatio: 1, // 4:3:1.333334 16:9:1.777778 1:1:1.0
              qrbox: {
                width: 280,
                height: 280
              },
            }, (decodedText, decodedResult) => {
            let codeUrl = decodedText; //获取扫码信息

            //这里进行自己的业务逻辑操作

             this.stopScan() // 结束扫码
              // alert("识别结果" + decodedText)
            }, errorMessage => {
            },
          )
            .catch(err => {});
        });
     },
     // 结束扫码
     stopScan() {
        this.html5QrCode.stop().then(() => {
          this.isScanning = false;
        })
     },
  }
</script>
<style lang='scss' scoped>
      /* 扫码样式 */
  .scan {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #000;

    .scan-box {
      width: 100%;
    }

    .close-scan {
      position: absolute;
      top: 20px;
      right: 20px;

      svg {
        width: 40px;
        height: 40px;
      }
    }
  }
  .start-scan{
    position: absolute;
    top: calc(50% - 25px);
    left: calc(50% - 100px);
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    width: 200px;
    height: 50px;
    background: #00aaff;
    color: #fff;
    border-radius: 10px;
  }
</style>

技术借鉴出处:

https://www.cnblogs.com/kinwai/p/17316033.html

  • 18
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
要在uni-app中使用html5-qrcode,可以按照以下步骤进行操作: 1. 安装html5-qrcode库:在uni-app项目的根目录下打开终端,执行以下命令安装html5-qrcode库: ``` npm install html5-qrcode ``` 2. 在需要使用扫码功能的页面的Vue文件中引入html5-qrcode库: ```javascript import Html5Qrcode from 'html5-qrcode'; ``` 3. 在页面的data中定义一个变量用于存储扫码结果: ```javascript data() { return { qrCodeResult: '' } } ``` 4. 在页面的methods中编写扫码相关的方法: ```javascript methods: { startScan() { const qrCodeScanner = new Html5Qrcode('qr-code-reader'); qrCodeScanner.start((result) => { this.qrCodeResult = result; qrCodeScanner.stop(); }, (error) => { console.error('扫码失败:', error); }, { fps: 10, qrbox: 250 }); } } ``` 5. 在模板中添加一个容器元素,用于显示扫码区域,并绑定相关事件: ```html <template> <view> <view id="qr-code-reader"></view> <button @tap="startScan">开始扫码</button> <view>{{ qrCodeResult }}</view> </view> </template> ``` 在以上示例代码中,我们首先通过`import`语句引入了html5-qrcode库。然后,在`data`中定义了一个`qrCodeResult`变量,用于存储扫码结果。接下来,在`methods`中编写了`startScan`方法,该方法会创建一个Html5Qrcode实例,并通过调用`start`方法开始扫码。当扫码成功时,会将结果赋值给`qrCodeResult`变量,并停止扫码。如果扫码失败,则会在控制台输出错误信息。 在模板中,我们添加了一个容器元素`<view id="qr-code-reader"></view>`,用于显示扫码区域。点击按钮时,会调用`startScan`方法开始扫码。扫码结果会通过插值表达式`{{ qrCodeResult }}`显示在页面上。 请注意,以上示例代码仅为演示如何在uni-app中使用html5-qrcode库开发扫码功能,实际使用时需要根据你的项目需求进行适当的调整。确保已正确安装和配置html5-qrcode库,并将其正确引入到uni-app项目中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值