微信小程序实现连续扫码的方案

一、使用微信小程序API里面的扫码,递归调用(此方法用户体验不好)

1.api如下

在这里插入图片描述

2.代码示例

2.1 index.wxml文件代码

<view class="page-body">
  <button bindtap='getScancode'>扫一扫</button>
</view>

2.2 index.ts文件代码(根据自己业务处理什么时候退出递归)

Page({
  getScancode() {
    wx.scanCode({
      success: (res) => {
        var result = res.result;
        wx.showToast({
          title:'扫码结果:'+result,
          icon:'none'
        })
        this.getScancode()
      }
    })
  }
})

二、使用微信小程序组件里面的camera(此方法识别率略差)

1.api如下

在这里插入图片描述

2.代码示例

2.1 index.wxml文件代码

<view class="page-body">
  <camera
      mode="scanCode"
      device-position="back"
      flash="off"
      bindscancode="onScanCode"
      binderror="binderror"
      style="width: 100%; height: calc(100vh)"
    ></camera>
</view>

2.2 index.ts文件代码

Page({
  onScanCode(e) {  
    console.log('扫码结果', e.detail.result);  
    wx.showToast({
      title:'扫码结果:'+e.detail.result,
      icon:'none'
    })
  }, 
  error(e) {
    console.log("e", e.detail);
  },
  // 用户不允许使用摄像头时触发
  binderror() {
    wx.showToast({
      icon: "none",
      title: "请授权摄像头",
    });
  }
  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值