收藏
回答

for循环,图片批量鉴黄?

我理想中的结果是一张一张图片鉴黄,把鉴定违规的图片删除,但是现在的结果如图:

//上传图片
onChangeFlockData: async function (e) {
    let imgUrls = e.detail.all; //上传的图片数组
    for (let i = 0; i < imgUrls.length; i++) {
      await this.imgInfoCheck(imgUrls[i].url)
    }
  },

//获取图片信息
  imgInfoCheck: async function (url) {
    wx.getImageInfo({
      src: url,
    }).then(async (res) => {
      console.log("图片信息:", res)
      const imgInfo = res.path;
      const imgWidth = res.width;
      const imgHeight = res.height;
      await this._compressImage(imgInfo, imgWidth, imgHeight);
    })
  },

//图片压缩
  _compressImage: async function (imgInfo, imgWidth, imgHeight) {
    const query = wx.createSelectorQuery()
    query.select('#canvas')
      .fields({
        node: true,
        size: true
      })
      .exec(res => {
        const canvas = res[0].node;
        const ctx = canvas.getContext('2d');
        const dpr = wx.getSystemInfoSync().pixelRatio;
        const imgW = Math.trunc(imgWidth / dpr);
        const imgH = Math.trunc(imgW / imgWidth * imgHeight);
        canvas.width = imgW;
        canvas.height = imgH;
        ctx.clearRect(0, 0, imgW, imgH);
        this.setData({
          canvasWidth: imgW,
          canvasHeight: imgH
        });
        let imageObj = canvas.createImage();
        imageObj.src = imgInfo;
        imageObj.onload = (res) => {
          ctx.drawImage(imageObj, 0, 0, imgW, imgH)
        };
        const cfgSave = {
          fileType: "jpg",
          quality: 0.5,
          width: imgW,
          height: imgH,
          destWidth: imgW,
          destHeight: imgH,
          canvas: canvas,
        };
        wx.canvasToTempFilePath({
          ...cfgSave,
        }).then(async res => {
          let tempUrl = res.tempFilePath
          console.log("压缩后图片:", res)
          await this.imgSecCheck(tempUrl)
        })
      })
  },


//图片送审
  imgSecCheck: async function (tempUrl) {
    wx.showLoading({
      mask: true
    });
    wx.getFileSystemManager().readFile({
      filePath: tempUrl,
      encoding: "base64",
      success: function (res) {
        let imgBuffer = res.data
        wx.cloud.callFunction({
          name: "msgSecCheck",
          data: {
            type: 'imgSecCheckBuffer', //以buffer方式上传送审
            value: imgBuffer,
          }
        }).then(res => {
          console.log("图片检测结果:", res.result.errCode)
          wx.hideLoading()
          if (res.result.errCode === 87014) {
            wx.hideLoading()
            wx.showToast({
              title: '图片含有违法违规内容',
              icon: 'none'
            })
            return
          }
          // this._uploadImg(tempUrl)
        }).catch(err => {
          console.log(err)
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '图片尺寸过大,请调整图片尺寸',
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        })
      },
      fail: err => {
        console.error(err);
      }
    })
  },
回答关注问题邀请回答
收藏

2 个回答

  • 津泓白了
    津泓白了
    发表于移动端
    2021-05-16
    😏😏😏
    2021-05-16
    有用
    回复
  • 北望沣渭
    北望沣渭
    2021-05-16

    for循环是同步循环,await不起作用,建议用Promise.all去并发。代码类似如下:

    Promise.all(imgUrls.map(
      async ({url}) => await this.imgInfoCheck(url)
    )).then(console.info);
    

    如果想一张接一张处理,for循环可以这么写:

    for await (let pic of imgUrls) {
      this.imgInfoCheck(pic.url);
    }
    
    2021-05-16
    有用
    回复 1
    • Qiu (吉²)
      Qiu (吉²)
      2021-05-16
      非常感谢🙏
      2021-05-16
      回复
登录 后发表内容