1.去百度注册应用,创建应用得到 key 和 Secret Key
2.调用百度云token接口https://aip.baidubce.com/oauth/2.0/token,得到Access Token
3.请求百度云人脸识别接口,传入AccessToken,请求参数如下:

{
    "image_type": "BASE64",
    "image": "/9j/4AAQSkZJR",
    "id_card_number": "131102********0653",
    "name": "***",
    "quality_control": "LOW",
    "liveness_control": "LOW",
    "spoofing_control": "LOW"
}

详见百度人脸识别文档:人脸识别

<view class="form_but"><button class="active" @click="onSubmit">提交</button></view>

 onSubmit(){ 
 var that = this
     uni.chooseImage({
      	count: 1,
      	sizeType: ['original', 'compressed'],
      	sourceType: ['camera'], //这要注意,camera掉拍照,album是打开手机相册
      	success: (res) => {
      		console.log(res);
      		const tempFilePaths = res.tempFilePaths;
      		uni.request({ // 强烈建议此接口由后端访问并且维护token有效期,否则前端每次访问都会刷新token
      					 //此url为专门插件测试预览用的key和secret key, 请替换为自己申请的key
      				url: 'https://aip.baidubce.com/oauth/2.0/token',
      				method: 'POST',
      				data: {
      					grant_type: 'client_credentials',
      					client_id: 'OqDUbDW1SYniKiwArRTZY1jb',
      					client_secret: '2S34jPGArDv1296IgsdG3LpkdDIs5oWh'
      				},
      				header: {
      					"content-type": "application/x-www-form-urlencoded"
      				},
      				success: (res) => {
      					console.log('访问成功');
      					//获取token
      					console.log('------------', res)
      					that.access_token = res.data.access_token
      					console.log('access', that.access_token)
      					console.log('tempFilePaths', tempFilePaths[0])
      					//图片转base64
      					pathToBase64(tempFilePaths[0]).then(path => {
      						that.baseImage = path.replace(/^data:image\/\w+;base64,/, "");
      						//console.log('baseImage',that.baseImage)
      						if(that.baseImage){
      						//对接百度云人脸识别
      						that.detect(that.baseImage)
      						}
 							//console.log('baseImage',that.baseImage)
      						//that.detect()
      						}).catch(error => {
      								console.error(error)
      						})	
      								console.log('baseurl', tempFilePaths[0])	
      							},
      							fail: (err) => {
      								console.log('访问失败');
      							}
      						});
      					}
      				});
       }
detect(obj) {
				var that = this
				uni.request({
					url: 'https://aip.baidubce.com/rest/2.0/face/v4/mingjing/verify?access_token=' + this.access_token,
					method: 'POST',
					data: {
						image:obj,
						image_type: "BASE64",
						id_card_number: this.form.cardNo,
						name:this.form.name,
						quality_control: "LOW",
						liveness_control: "LOW",
						spoofing_control: "LOW"
					},
					header: {
						"content-type": "application/json"
					},
					success: (res) => {
						console.log('访问成功:', res);
						if(res.data.error_code){
							console.log('222222222')
							uni.showToast({
								icon: 'error',
								title: '人脸检测未通过!'
							})
						}else{
							if(res.data.result.score>=80){
								console.log('1111111111')
								uni.showToast({
									icon: 'success',
									title: '人脸检测通过!'
								})
								that.sure()
							}else{
								console.log('3333')
								uni.showToast({
									icon: 'error',
									title: '人脸检测未通过!'
								})
							}
						}
						
					},
					fail: (err) => {
						console.log('访问失败:', err);
						uni.showToast({
							icon: 'none',
							title: '人脸检测调用失败!'
						})
					}
				});
			},
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐