当前位置:网站首页>小程序调用百度api实现图像识别
小程序调用百度api实现图像识别
2022-08-09 08:22:00 【php的绯闻女友】
一、获取app_key和secret_key
1.1首先需要去百度智能云搜索图像识别
1.2创建应用--领取免费资源(我已经创建了应用,这个是可以免费使用一年的,测试够了)
1.3获取app_key和secret_key
点进上一张图的管理应用即可查看相应信息
二、获取token
2.1首先判断storage中是否已经有token
onLoad() {
// 自定义头部导航栏文字
wx.setNavigationBarTitle({
title: '垃圾搜索'
});
//获取storge中的token
var that=this;
wx.getStorage({
key:'expires_in',
success(res){
// 获取成功,证明本地已存有相关token
const newT =new Date().getTime();
// // 用当前时间和存储的时间判断,token是否已过期
if (newT > parseInt(res.data)) {
// token过期,重新获取token
that.getToken();
} else {
// 获取本地缓存的token
let token=wx.getStorageSync('access_token');
that.setData({token:token});
}
},fail(){
that.getToken();
}
});
},
2.2 getToken函数
// 获取token
getToken:function(){
var that=this;
// client_id:app_key需要自己去百度智能云创建相应的应用后获取
//client_secret:Secret Key
const url = 'https://aip.baidubce.com/oauth/2.0/token'+
'?grant_type=client_credentials'+
'&client_id=app_key&client_secret=client_secret';
wx.request({
url:url,
method: 'POST',
success: res => {
console.log(res)
let thaRres=res.data;
// 将access_token存储到storage中
wx.setStorage({
key:'access_token',
data:thaRres.access_token
});
var date=new Date().getTime();
let time=date+2592000*1000;
console.log('三十天后的时间',time);
console.log('当前时间戳',date)
wx.setStorage({
key:'expires_in',
data:time
});
that.setData({token:thaRres.access_token});
/*
access_token: 要获取的Access Token;
expires_in: Access Token的有效期(秒为单位,一般为1个月);
*/
},
fail: () => {}
});
},
三、进行图像识别
// 点击相机图标时
cameraTap:function(){
console.log('点击相机');
var that=this;
wx.chooseImage({
success: res => {
wx.getFileSystemManager().readFile({
filePath: res.tempFilePaths[0], //选择图片返回的相对路径
encoding: 'base64', //编码格式
success: res => { //成功的回调
// 图片
wx.showLoading({
title: '识别中...',
})
var image = res.data;
// 文字识别
wx.request({
url: 'https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general?access_token='+that.data.token,
data: {
image: image
},
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method: 'POST',
success(res) {
console.log(res.data)
wx.hideLoading({
success: (res) => {},
})
//此处接处理识别结果的函数,可见下一篇文章:https://blog.csdn.net/Moxi099/article/details/120617526?spm=1001.2014.3001.5501
},
fail:()=>{
wx.hideLoading();
wx.showToast({
title: '图片识别失败,请重试!',
icon: 'none'
});
}
})
}
})
}
})
},
至此大功告成,返回的数据如下:
result就是最终需要的数据了!
边栏推荐
- 继承中的运算符重载:输入输出的传奇
- ASEMI整流桥GBJ810参数,GBJ810封装,GBJ810重量
- Servlet的实现原理解析(serverapplet)(服务端程序)
- pragma comment的使用(转载)重新排版
- jdbctemplate connects to sql server, the data found in the code is inconsistent with the database, how to solve it?
- Different styles of Flask-restful
- The story of the disappearing WLAN of Windows 10 computers
- Collection 接口 & List 接口
- SQL存储过程
- 897. Increasing Order Search Tree
猜你喜欢
EMQ X message server learning record - prepare for the subsequent completion
eTS UI development learning
IO byte stream reads text Chinese garbled
数制转换及子网划分
VMware virtual machine cannot be connected to the Internet after forced shutdown
【愚公系列】2022年08月 Go教学课程 033-结构体方法重写、方法值、方法表达式
Result consisted of more than one row
网络层协议介绍
监视文本框的输入
三次握手,四次挥手
随机推荐
Cookie和Session详解
Shell--常用小工具(sort、uniq、tr、cut)
pip3 source change to improve speed
App testing
GBJ610-ASEMI超薄整流扁桥GBJ610
Database MySQL installation and uninstallation
Go语言技巧之正确高效使用slice(听课笔记总结--简单易懂)
897. Increasing Order Search Tree
编程洗衣机:字符串输出后的乱码
System Security and Application
泛型和lambda表达式
浅谈Flask_script
File Handling (IO)
The Servlet,
leetcode 35. 搜索插入位置(二分法+找性质也很关键)
The principle and configuration of VLAN
Cookie and Session Details
Introduction to the Endpoint
继承中的运算符重载:输入输出的传奇
Shell programming loop statement and function