微信小程序相机自动识别去掉拍照的快门声

无快门声的实现方式:

// 识别相关
  createListener(){
    let num = 0,self = this;
    const context = wx.createCameraContext();
    listener = context.onCameraFrame(async function (res) {
        num++;
        if (num === 50) {
          num = 0;
          self.data2Base64(res)
        } 
    });
    listener.start();
  },
  data2Base64(frame){
    var self = this;
    const query = wx.createSelectorQuery();
    query.select('#helperCanvas')
          .fields({node: true, size: true})
          .exec((res) => {
            const {width,height} = frame;
            const data = new Uint8Array(frame.data);
            const node = res[0].node
            const ctx = node.getContext('2d');

            const imgData = node.createImageData(data,width,height);
            node.width = imgData.width;
            node.height = imgData.height;

            ctx.putImageData(imgData,0,0);
            wx.canvasToTempFilePath({
              canvas: node,
              success(res){
                self.upload(res.tempFilePath)// 上传图片到识别服务器
              }
            })
          })
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    canNextRec = true;
    if (listener) {
      listener.start();
    } else { 
      this.createListener();
    }
  },
    /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    listener && listener.stop();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    listener && listener.stop();
  },

有快门声的实现


  timerReco(){
    cameraContext  = wx.createCameraContext();
    timer = setInterval(() => {
      this.takePhoto();
    },1000);
  },
  takePhoto() {
    if (!canNextRec) return;
    canNextRec = false;
    var self = this;
    cameraContext.takePhoto({
      success: (imageInfo) => {
        self.upload(imageInfo.tempImagePath);
      }
    })
  },
  upload: function (picpath) {
    var self = this;
    wx.uploadFile({
      url: app.globalData.urlHeader + api.identify,
      filePath: picpath,
      name: 'file',
      header: {
        'content-type': 'multipart/form-data',
        'XSESSIONID': app.globalData.token
      },
      formData: {
        "x": 0,
        "y": 0,
        "kind": '2'
      },
      success: function (res) {
        if (res.data) {
          var info = JSON.parse(res.data);
          console.log('识别结果',info);
          canNextRec = true;
        }
      },
      fail: (res) => {
        canNextRec = true;
        wx.hideLoading();
      },
      
      complete: function (res) {
      }
    })
  }, 
   /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     this.timerReco();
  },
    /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(timer);
  },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值