JS 获取图片、文件数据并封装在json中

	var objFile = document.getElementById("img_data");
	var formData = new FormData(objFile);
    $.ajax({
          url: 'http://localhost:8080/big/gogo?type=save_img',
          type: "post",
          data: formData, 
          cache: false,  
          contentType: false,
          processData: false
      });

作用对象

<input type="file" name="head" id="test" value="上传图片">

当然 img 标签也一样

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
示例代码: 1. 在小程序页面引入百度AI的SDK,并初始化: ``` const baiduAI = require('../../utils/baiduAI.js'); const baiduClient = new baiduAI.BaiduClient('YourAppId', 'YourAPIKey', 'YourSecretKey'); ``` 2. 在页面添加一个拍照按钮: ``` <button bindtap="takePhoto">拍照识别</button> ``` 3. 定义一个takePhoto方法,用于调用摄像头并发送识别请求: ``` takePhoto: function() { wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['camera'], success: (res) => { const tempFilePath = res.tempFilePaths[0]; const param = { image: baiduAI.base64Encode(tempFilePath), baike_num: 5 }; baiduClient.getObject(param).then(result => { console.log(result); // 处理识别结果 }).catch(error => { console.error(error); }); }, fail: (res) => { console.error(res); } }) } ``` 4. 定义一个baiduAI.js文件,用于封装百度AI的API调用: ``` const BaiduClient = function(appId, apiKey, secretKey) { this.appId = appId; this.apiKey = apiKey; this.secretKey = secretKey; this.authToken = ''; }; BaiduClient.prototype.getAuthToken = function() { return new Promise((resolve, reject) => { wx.request({ url: 'https://aip.baidubce.com/oauth/2.0/token', data: { grant_type: 'client_credentials', client_id: this.apiKey, client_secret: this.secretKey }, success: (res) => { if (res.statusCode === 200) { this.authToken = res.data.access_token; resolve(res.data.access_token); } else { reject(`获取access_token失败,错误码:${res.statusCode}`); } }, fail: (res) => { reject(`获取access_token失败,${res.errMsg}`); } }) }) }; BaiduClient.prototype.getObject = function(param) { if (!this.authToken) { return this.getAuthToken().then(() => { return this.getObject(param); }); } return new Promise((resolve, reject) => { wx.request({ url: `https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general?access_token=${this.authToken}`, method: 'POST', data: param, header: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: (res) => { if (res.statusCode === 200) { resolve(res.data); } else { reject(`识别失败,错误码:${res.statusCode}`); } }, fail: (res) => { reject(`识别失败,${res.errMsg}`); } }) }) }; const base64Encode = function(file) { const bitmap = wx.getFileSystemManager().readFileSync(file); return wx.arrayBufferToBase64(bitmap); }; module.exports = { BaiduClient, base64Encode }; ``` 注意:需要在百度AI平台上申请access_token,并填写到代码的getAuthToken方法。另外,还需要在小程序的app.json文件添加百度AI的域名白名单。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值