小程序和fetch使用百度云图片识别 失败:"image format error"、"param image not exist"

小程序使用百度云图片识别——失败

“image format error”

注意!注意!注意!

  1. 去掉base64字符串前面的图片信息头data:image/jgp;base64,
  2. 不要进行什么urlencode编码
    当上面注意中的内容你满足一项时就会出现下图的错误:
    小程序使用报"image format error"截图

代码示例(可直接复制使用,记得修改你的access_token)

//选择图片
image_classify: function() {
  let that=this;
  wx.chooseImage({
    success(res) {
      that.urlTobase64(res.tempFilePaths[0]);
    }
  })
},
urlTobase64:function(url) {
  wx.request({
    url: url,
    responseType: 'arraybuffer', //最关键的参数,设置返回的数据格式为arraybuffer
    success: res => {
      //把arraybuffer转成base64
      let base64 = wx.arrayBufferToBase64(res.data);
      // console.log(base64)
      wx.request({
        url: 'https://aip.baidubce.com/rest/2.0/image-classify/v1/plant?access_token=你自己的access_token',
        data:{
          image:base64
        },
        method:'post',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success(res) {
          console.log(res)
        }
      })
    }
  })
},

代码执行结果: 小程序使用百度云图片识别代码执行结果

fetch使用百度云图片识别——失败

“param image not exist”

官方规定Content-Type格式为application/x-www-form-urlencoded,body传值时千万要注意!!!需要给body传一个键值对构成的字符串:例如:body:"user=哈哈&name=18",以下两种传值方式都会出现"param image not exist"

第一种错误传值:
body错误传值第一种
第二种错误传值:
body错误传值第二种
以上两种传值方式执行结果:
"param image not exist"错误截图

“image format error”

  1. 去掉base64字符串前面的图片信息头data:image/jgp;base64,

  2. 根据官网要求进行urlencode编码,使用encodeURIComponent编码,而不是encodeURI,使用encodeURI会出现"image format error"错误。

    encodeURI编码代码截图:
    使用编码代码截图

    encodeURI编码出现"image format error"
    编码执行结果

代码示例

_Classify() {
	let url='https://aip.baidubce.com/rest/2.0/image-classify/v1/plant?access_token=你自己的access_token';
	let options={
		"method": "post",
		"Content-Type": "application/x-www-form-urlencoded",
	};
	//ImagePicker是react-native选取图片的插件
	ImagePicker.openPicker({
		includeBase64: true
	}).then(image => {
		let img = encodeURIComponent(image.data);
		options.body=`image=${img}`;
		fetch(url,options)
		.then((response) => response.json())
		.then((responseJson) => {
			console.log(responseJson)
		})
	}).catch(error => {});
}

代码执行结果:
fetch使用百度云图片识别代码执行结果

encodeURI()encodeURIComponent()

encodeURI()encodeURIComponent()方法可以对URI进行编码,以便发送给浏览器。有效的URI中不能包含某些字符,例如空格等。

encodeURI()

  1. 使用

    encodeURI(URIstring);
    
  2. 说明
    该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。该方法的目的是对 URI 进行完整的编码,因此对以下在 URI 中具有特殊含义的 ASCII 标点符号,encodeURI() 函数是不会进行转义的:;/?:@&=+$,#

  3. 测试

    console.log(encodeURI("https://aip.baidubce.com"))
    console.log(encodeURI("https://aip.baidubce.com/rest/2.0/image-classify /v1/plant"))
    console.log(encodeURI(",/?:@&=+$#"))
    

    encodeURI编码测试结果:
    encodeURI编码测试结果

encodeURIComponent()

  1. 使用

    encodeURIComponent(URIstring);
    
  2. 说明
    该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( )
    其他字符(比如 :;/?:@&=+$,# 这些用于分隔 URI 组件的标点符号),都是由一个或多个十六进制的转义序列替换的。

  3. 测试

    console.log(encodeURI("https://aip.baidubce.com"))
    console.log(encodeURI("https://aip.baidubce.com/rest/2.0/image-classify /v1/plant"))
    console.log(encodeURI(",/?:@&=+$#"))
    

    encodeURIComponent编码测试结果:
    encodeURIComponent编码测试结果

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值