目前项目有个需求,需要在微信小程序里实现一个自定义相机,然后用户可操作来拍照,最后将拍出来的照片进行人脸识别
<template>
<view>
<camera :device-position="devicePosition" flash="off" @error="error" style="width: 100%; height: 300px;"></camera>
<button type="primary" @click="takePhoto">拍照</button>
<button type="primary" @click="toggleDevicePosition">翻转摄像头</button>
<view>预览</view>
<image mode="widthFix" :src="src"></image>
</view>
</template>
<script>
export default {
data () {
return {
src: "",
cameraCtx: null,
devicePosition: 'backs' //前置或后置摄像头,值为front, back
}
},
onLoad () {
this.cameraCtx = uni.createCameraContext();
},
methods: {
takePhoto () {
this.cameraCtx.takePhoto({
quality: 'high',
success: (res) => {
this.src = res.tempImagePath
}
});
},
toggleDevicePosition () {
this.devicePosition = this.devicePosition == 'back' ? 'front' : 'back'
},
error (e) {
console.log(e.detail);
}
}
}
</script>
```
最后将拍摄到的图片通过人脸识别接口请求对比一下就行了