收藏
回答

wx.canvasToTempFilePath花费的时间为什么越来越长?

需求是,通过监听onCameraFrame方法不断获取相机实时帧数据转换成jpg图片并不断上传,采用如下代码方法,现在发现因为 wx.canvasToTempFilePath花费的时间越来越长,导致画面越来越卡。。。。。。。。图一刚开始,图二是中间,图三是十秒后


    this.listener = camera_ctx.onCameraFrame((frame) => {
        // nCounter等于30 是因为一开始相机会有一个对焦的过程,如果一开始获取数据,就是模糊的图片
        if (nCounter == 10) {
          // console.log('nCounter+30结束');
          timeadd2 = new Date()
          console.log('nCounter+30时间为: ');
          console.log(this.timeFormat(timeadd1, timeadd2));
          console.log('实时帧高宽度-----------------------');
          console.log('宽度:' + frame.width + ' ' + '高度: ' + frame.height)
          //将实时帧像素数据格式转换成Uint8ClampedArray
          var data = new Uint8Array(frame.data);
          var clamped = new Uint8ClampedArray(data);
          // 实时帧数据添加到Canvas上
           //将实时帧数据添加到画布
          getImgData(data, width, height) {
    // console.log('进入到将像素数据添加到画布');
    // console.log(this.timeFormat(new Date()));
    timecanva1 = new Date()
    wx.canvasPutImageData({
      canvasId'firstCanvas',
      x0,
      y0,
      width: width,
      height: height,
      data: data,
      successres => {
        // console.log('成功将像素数据添加到画布----------');
        // console.log(this.timeFormat(new Date()));
        timecanva2 = new Date()
        console.log('像素数据添加到画布时间为: ');
        console.log(this.timeFormat(timecanva1, timecanva2));
        // console.log(res);
          //将画布移动到临时图片
  getImgPath(width, height) {
    // console.log('进入canvans转换为临时图片');
    // console.log(this.timeFormat(new Date()));


    timeImg1 = new Date()
    let that = this
    wx.canvasToTempFilePath({
      x0,
      y0,
      width: width,
      height: height,
      canvasId'firstCanvas',
      fileType'jpg',
      destWidth: width,
      destHeight: height,
      // 图片的质量
      quality0.1,
      successres => {
        that.canvasContext.clearRect(00, width, height);
        console.log('清除后canvasContext-------------------');
        console.log(that.canvasContext);
        // console.log('成功将canvans转换为临时图片------------');
        // console.log(this.timeFormat(new Date()));
        timeImg2 = new Date()
        console.log('canvans转换为临时图片时间为:');
        console.log(this.timeFormat(timeImg1, timeImg2));
        that.setData({
          restempFilePath: res.tempFilePath
        });
        that.upLoadImg(res.tempFilePath)
      },
      fail(res) {
        console.log('获取canvans图片失败');
        console.log(res);
      }
    }, this)


  },
      }
    })
  },
        }
        nCounter++
      })


最后一次编辑于  2021-04-16
回答关注问题邀请回答
收藏

2 个回答

  • 穆清
    穆清
    2023-05-24

    同问

    2023-05-24
    有用
    回复
  • enchantedwyf
    enchantedwyf
    2021-11-08

    请问有解决吗,我也碰到了这个问题,用canvans画图后,时间越来越久

    2021-11-08
    有用
    回复
登录 后发表内容
问题标签