const app = getApp() Page({ data: { canIUse: wx.canIUse('button.open-type.getUserInfo'), src: "", //图片的链接 baidutoken: "", base64: "", msg: "" }, //拍照 takePhoto() { var that = this; //拍照 const ctx = wx.createCameraContext() ctx.takePhoto({ quality: 'high', success: (res) => { this.setData({ src: res.tempImagePath //获取图片 }) //图片base64编码 wx.getFileSystemManager().readFile({ filePath: this.data.src, //选择图片返回的相对路径 encoding: 'base64', //编码格式 success: res => { //成功的回调 this.setData({ base64: res.data }) } }) this.getBaiduToken(); } //拍照成功结束 }) //调用相机结束 //失败尝试 wx.showToast({ title: '请重试', icon: 'loading', duration: 500 }) }, error(e) { console.log(e.detail) }, getBaiduToken() { var that = this; //acess_token获取,qs:需要多次尝试 wx.request({ url: 'https://aip.baidubce.com/oauth/2.0/token', //是真实的接口地址 data: { grant_type: 'client_credentials', client_id: 'xxxxxxxxxxxxxxxxxxx', //用你创建的应用的API Key client_secret: 'xxxxxxxxxxxxxxxxxxx' //用你创建的应用的Secret Key }, header: { 'Content-Type': 'application/json' // 默认值 }, success(res) { that.setData({ baidutoken: res.data.access_token //获取到token }) that.uploadPhoto(); } }) }, uploadPhoto() { var that = this; //上传人脸进行注册-----test wx.request({ url: 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token=' + this.data.baidutoken, method: 'POST', data: { image: this.data.base64, image_type: 'BASE64', group_id: 'xh_0713', //自己建的用户组id user_id: 'xiehao' //这里获取用户昵称 }, header: { 'Content-Type': 'application/json' // 默认值 }, success(res) { that.setData({ msg: res.d
时间: 2025-06-25 22:18:34 浏览: 14
### 百度AI人脸识别功能在微信小程序中的具体实现
#### 获取Token
为了调用百度AI的相关接口,首先需要获取访问令牌(Access Token)。这可以通过API Key和Secret Key来完成。这些密钥可以在百度智能云的管理控制台中找到[^3]。
```javascript
wx.request({
url: 'https://aip.baidubce.com/oauth/2.0/token',
method: 'POST',
data: {
grant_type: 'client_credentials',
client_id: 'your_api_key', // 替换为实际的API Key
client_secret: 'your_secret_key' // 替换为实际的Secret Key
},
success(res) {
const accessToken = res.data.access_token;
console.log('Access Token:', accessToken);
}
});
```
#### 拍摄照片并上传至百度云
在微信小程序中可以利用`<camera>`组件让用户拍摄照片,并将图片转换成Base64编码以便上传到百度云端进行处理[^1]。
```html
<view>
<button bindtap="takePhoto">拍照</button>
<image src="{{src}}" mode="aspectFit"></image>
</view>
```
```javascript
Page({
data: {
src: ''
},
takePhoto() {
const ctx = wx.createCameraContext();
ctx.takePhoto({
quality: 'high',
success: (res) => {
this.setData({ src: res.tempImagePath });
this.uploadImageToBaidu(res.tempImagePath); // 调用上传函数
}
});
},
uploadImageToBaidu(filePath) {
wx.getFileSystemManager().readFile({
filePath,
encoding: 'base64',
success: (result) => {
const imageBase64 = result.data;
wx.request({
url: `https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=${accessToken}`,
method: 'POST',
header: { 'Content-Type': 'application/json' },
data: JSON.stringify({
image: imageBase64,
image_type: 'BASE64',
face_field: 'age,gender,glasses'
}),
success(response) {
console.log('Face Detection Result:', response.data);
}
});
}
});
}
});
```
上述代码展示了如何从相机捕获图像并将该图像作为Base64字符串发送给百度AI服务器以执行面部检测。
#### 常见问题及解决方案
1. **错误码返回**:如果请求失败,通常会收到一个带有特定错误码的响应。查阅官方文档可以帮助理解这些错误的原因以及相应的修复措施。
2. **权限不足**:确保已授予足够的权限用于网络请求和文件读取操作。
3. **超时设置**:对于较慢的网络环境考虑增加timeout参数以防中断连接。
阅读全文
相关推荐


















