微信小程序云开发问题篇2_微信小程序调百度图像识别接口问题小计(报错:216101 not enough param 参数不足、上传到云存储图片名称后缀问题、图像base64编码识别)

目录
  • 报错216101 not enough param 参数不足
  • 上传的图片到云存储名称后缀问题
  • 图像base64编码识别

一、问题描述1-报错216101 not enough param 参数不足

拿到百度接口的access_token之后,使用下面函数访问api,查看response,提示 参数不足

//5. 访问api

const askApiUrl = function (img_base){
  return new Promise((resolve, reject) => {
    // 2. 访问api
    var API_URL = 'https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination?'

    wx.request({
      url: API_URL + wx.getStorageSync('access_token'),
      // access_token:,
      data: {
        imageUrl: 'https://image.so.com/i?q=%E5%9B%BE%E7%89%87&listsrc=sobox&listsign=214eb5ce90dd82904e08ceaeb513b8bd&src=360pic_new_strong',
        scenes: ['advanced_general']
      },
      header: {
        'Content-Type': 'application/json;charset=utf-8' // 默认值
      },
      dataType: "json",
      method: "POST",
      success: function (res) {
        console.log(res)
        resolve(res);
      },
      fail: function (res) {
       
        wx.showToast({
          title: '网络错误,请重试!',
          icon: 'none',
          duration: 2000
        })
        reject(res);
      },
      complete: function (res) {
        resolve(res);
      }
    })
  })

文档上要求的access_token也加上了,图片链接也应该没问题啊,图片有问题的话会报 image empty的错,应该不是图片找不到的问题

还得是access_token的问题,试错几次,最后终于找到问题所在。

问题解决

低级错误,?参数key = 参数value

在这里插入图片描述

二、问题描述2-上传的图片到云存储名称后缀问题

因为图像类型可以为·png .img ...,需要动态的指定上传至 云存储 的类行,而且图像的名称也是个问题

 wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        wx.showLoading({
          title: '上传检索中',
        })
        // 1. 暂时文件地址
        const tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths)
        // 2. base64格式
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0],
          encoding: "base64",
          success: function (data) {
            console.log(data.data)
            wx.setStorageSync('img_base', data.data)
          }
        })
        // 3. 照片后缀
        var arr = tempFilePaths[0].split(".")

        // 4. 上传文件至云存储
        wx.cloud.uploadFile({
          cloudPath: "imgs/" + new Date().getTime().toString() + "." + arr[1],
          filePath: tempFilePaths[0].toString(),
          // 4.1成功上传
          success: res => {
            wx.showToast({
              title: '上传成功',
              icon: 'none',
              duration: 2000
            })
          ...
          ...
问题解决

控制台输出 选择图片的临时地址tempFilePaths[0](因为可以上传多个照片一次,因此为数组)

格式为 :http://tmp/iJnF10XFTaQQb12d030c8ee6ce2c565ea365ae39f818.jpg

  • 因此后缀问题可以split切割拿到 . 后面的后缀 :var arr = tempFilePaths[0].split(".")
  • 而图片名称的问题可以采用时间戳的形式:cloudPath: "imgs/" + new Date().getTime().toString() + "." + arr[1],
三、问题描述3-关于百度图像识别接口image需要base64编码后的图片

base64是一种压缩编码算法,就是一串字符串,图像识别接口需要这种类型的 图片 进行识别

也可以选择imgUrl ,只不过云开发的话,照片传至 云存储 通过url 不太好拿出来 ,因此方便的话就是使用工具对图片base64编码

在这里插入图片描述

问题解决
 wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        wx.showLoading({
          title: '上传检索中',
        })
        // 1. 暂时文件地址
        const tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths)
        // 2. base64格式
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0],
          encoding: "base64",
          success: function (data) {
            console.log(data.data)
            wx.setStorageSync('img_base', data.data)
          }
        })

...
...

wx.getFileSystemManager().readFile({})filePath参数为 选择图片的临时地址 tempFilePaths[0]

相比将图片存储到其他服务器,更方便一些

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

scl、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值