当前位置:
首页
文章
前端
详情

微信小程序扫描二维码和条形码

微信小程序提供了wx.scanCode来处理扫描功能

使用方式:

// 允许从相机和相册扫码
wx.scanCode({
  success(res) {
    console.log(res)
  }
})

// 只允许从相机扫码
wx.scanCode({
  onlyFromCamera: true,
  success(res) {
    console.log(res)
  }
})

示例:

index.wxml

<view class='form-list'>
  <text>运单号码</text>
  <input type='text' value='{{scanCodeMsg}}'></input>
  <image class='scan' bindtap='scanCode' src='/images/scanCode.png' mode='widthFix'></image>
</view>

给扫描按钮的图片绑定一个事件,点击调用摄像头扫码,扫描成功将数值赋给 input 输入框的 value 值。

index.js部分代码

data: {
  scanCodeMsg: "",
},
scanCode: function() {
  var that = this;
  wx.scanCode({ // 扫描API
    success(res) { // 扫描成功
      console.log(res) // 输出回调信息
      that.setData({
        scanCodeMsg: res.result
      });
      wx.showToast({
        title: '成功',
        duration: 1000
      })
    }
  })
}

参考链接:

  • 官方文档:https://developers.weixin.qq.com/miniprogram/dev/api/device/scan/wx.scanCode.html
  • https://www.w3h5.com/post/255.html

免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。