一、使用微信小程序API里面的扫码,递归调用(此方法用户体验不好)
1.api如下
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/aa7878b21ee647dab84d4389455df6d9.png)
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如下
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/dd9887df96184223b4022e04264d248c.png)
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: "请授权摄像头",
});
}