会创建小程序项目和小程序页面
知道小程序页面的组成部分及各自的作用
会用 button,image,view 等小程序组件
会使用 wx.request() 发起网络数据请求
箭头函数
let,const 等
<camera style="height: {{wh}}px; width: 100%;" flash="off">camera>
在 data 中定义 wh
data: {
// 窗口可用的高度
wh: 0
}
动态获取页面可用高度
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
const sysInfo = wx.getSystemInfoSync()
this.setData({
wh: sysInfo.windowHeight
})
}
在 app.json 的 window 节点中,新增如下配置:
{
"pages": [
"pages/home/home"
],
"window": {
// ... 省略其他配置
"navigationStyle": "custom"
},
"sitemapLocation": "sitemap.json"
}
<camera style="height: {{wh}}px; width: 100%;" flash="off">
<cover-view class='btn-box'>
<cover-image src='/images/icon/reverse.png'>cover-image>
<cover-image src='/images/icon/camera.png'>cover-image>
<cover-image src='/images/icon/album.png'>cover-image>
cover-view>
camera>
.btn-box {
display: flex;
justify-content: space-around;
position: absolute;
bottom: 50px;
width: 100%;
}
.btn-box cover-image {
width: 50px;
height: 50px;
opacity: 0.7;
}
data: {
// 摄像头的朝向 front back
position: 'front'
}
<cover-image src='/images/icon/reverse.png' bindtap='reverseCamera'>cover-image>
// 点击按钮,切换摄像头
reverseCamera() {
const newPosition = this.data.position === 'front' ? 'back' : 'front'
this.setData({
position: newPosition
})
}
device-position
<camera style="height: {{wh}}px; width: 100%;" flash="off" device-position='{{position}}'>camera>
data: {
// 照片的路径
src: ''
}
<cover-image src='/images/icon/camera.png' bindtap='takePhoto'>cover-image>
// 拍照
takePhoto() {
// 创建相机的实例对象
const ctx = wx.createCameraContext()
// ctx.takePhoto 实现拍照
ctx.takePhoto({
quality: 'high',
success: (res) => {
// console.log(res.tempImagePath)
this.setData({
src: res.tempImagePath,
isShowPic: true
}, () => {
this.getFaceInfo()
})
},
fail: () => {
console.log('拍照失败!')
this.setData({
src: ''
})
}
})
}
<cover-image src='/images/icon/album.png' bindtap='choosePhoto'>cover-image>
// 从相册选取照片
choosePhoto() {
wx.chooseImage({
count: 1,
sizeType: ['original'],
sourceType: ['album'],
success: (res) => {
// console.log(res)
if (res.tempFilePaths.length > 0) {
this.setData({
src: res.tempFilePaths[0],
isShowPic: true
}, () => {
this.getFaceInfo()
})
}
},
fail: () => {
console.log('选择照片失败!')
this.setData({
src: ''
})
}
})
}
<view wx:else>
<image src='{{src}}' style='width: 100%; height: {{wh}}px; display: block;' mode='aspectFill'>image>
view>
<button type='warn' class='reChoose' bindtap='reChoose'>重选照片button>
// 重新选择照片
reChoose() {
this.setData({
isShowPic: false,
src: ''
})
}
http://ai.baidu.com/
API Key
和 Secret Key
// this.globalData.access_token = 'aaa'
wx.request({
method: 'POST',
url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=自己的ID&client_secret=自己的KEY',
success: (res) => {
this.globalData.access_token = res.data.access_token
},
fail: () => {
wx.showToast({
title: '鉴权失败!',
})
}
})
const fileManager = wx.getFileSystemManager()
const fileStr = fileManager.readFileSync(this.data.src, 'base64')
wx.request({
method: 'POST',
url: 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' + token,
header: {
'Content-Type': 'application/json'
},
data: {
image_type: 'BASE64',
image: fileStr,
// 年龄,颜值分数,表情,性别,是否戴眼镜,情绪
face_field: 'age,beauty,expression,gender,glasses,emotion'
},
success: (res) => {
console.log(res)
if (res.data.result.face_num <= 0) {
return wx.showToast({
title: '未检测到人脸!',
})
}
this.setData({
faceInfo: res.data.result.face_list[0],
isShowBox: true
})
},
fail: () => {
wx.showToast({
title: '颜值检测失败!',
})
},
complete: () => {
wx.hideLoading()
}
})
定义映射关系:
data: {
// 映射关系
map: {
gender: {
male: '男', female: '女'
},
expression: {
none: '不笑', smile: '微笑', laugh: '大笑'
},
glasses: {
none: '无眼镜',common: '普通眼镜',sun: '墨镜'
},
emotion: {
angry: '愤怒', disgust: '厌恶', fear: '恐惧', happy: '高兴',
sad: '伤心', surprise: '惊讶', neutral: '无情绪'
}
}
}
修改UI结构:
<view class='faceinfo_box' wx:if="{{isShowBox}}">
<view class='face_row'>
<text>年龄:{{faceInfo.age}}岁text>
<text>性别:{{map.gender[faceInfo.gender.type]}}text>
view>
<view class='face_row'>
<text>颜值:{{faceInfo.beauty}}分text>
<text>表情:{{map.expression[faceInfo.expression.type]}}text>
view>
<view class='face_row'>
<text>眼镜:{{map.glasses[faceInfo.glasses.type]}}text>
<text>情绪:{{map.emotion[faceInfo.emotion.type]}}text>
view>
view>