使用腾讯云paas服务接口通过视频进行活体校验-人脸识别

使用腾讯云paas服务接口通过视频进行活体校验-人脸识别

在这里插入图片描述

在这里插入图片描述

需求

输入姓名,身份证号,调取微信camera摄像机拍摄视频,并把视频转换成base64格式,上传
IdCard 是 String 身份证号
Name 是 String 姓名。中文请使用UTF-8编码
VideoBase64 否 String 用于活体检测的视频,视频的BASE64值;
BASE64编码后的大小不超过8M,支持mp4、avi、flv格式
三个参数调取腾讯人脸识别接口,腾讯识别接口会从视频中截取一张图片进行身份校验

1.先判断是否授权摄像机,录音机权限

<template>
	<view class="face-wrap">
		<view class="face-from">
			<view class="form-item">
				<view class="form-item-title">真实姓名</view>
				<view class="form-item-con">
					<input
						type="text"
						@input="getCardNameHandle"
						v-model="identityObject.cardname"
						class="form-item-input"
						placeholder="请输入真实姓名"
						placeholder-class="placeholderclass"
					/>
				</view>
			</view>
			<view class="form-item">
				<view class="form-item-title">身份证号码</view>
				<view class="form-item-con">
					<input
						type="number"
						@input="getCardIDHandle"
						v-model="identityObject.cardid"
						class="form-item-input"
						placeholder="请输入对应的身份证号码"
						placeholder-class="placeholderclass"
					/>
				</view>
			</view>
		</view>
		<view v-if="isSubmit" class="form-btn" @click="faceRecognitionHandle">点击开始人脸识别</view>
		<view v-else class="form-disabled">点击开始人脸识别</view>
	</view>
</template>

<script>
import Utils from '@/common/js/utils.js';
export default {
	data() {
		return {
			identityObject: {
				cardname: '', //姓名
				cardid: '' //身份证
			}
		};
	},
	onLoad(options) {
		
	},
	onShow() {},
	methods: {
		/**获取姓名**/
		getCardNameHandle(e) {
			let cardname = Utils.trim(e.detail.value);
			this.identityObject['cardname'] = cardname;
		},
		/**获取身份证号**/
		getCardIDHandle(e) {
			let cardid = Utils.trim(e.detail.value);
			this.identityObject['cardid'] = cardid;
		},
		/**点击人脸识别**/
		faceRecognitionHandle() {
			const identityObject = this.identityObject;
			if (!(identityObject.cardname.length > 0 && identityObject.cardid.length > 0)) {
				uni.showToast({
					title: '不能为空',
					icon: 'none',
					mask: true
				});
				return false;
			}
			if (!Utils.isCardID(identityObject.cardid)) {
				uni.showToast({
					title: '证件号错误',
					icon: 'none',
					mask: true
				});
				return false;
			}
			/**
			 * 开始人脸识别
			 * 需求:使用视频验证
			 * **/
			/**
			 * 1.先判断是否授权摄像头,录音
			 * **/
			this.jsAuthorizeAPI('scope.camera', 1);
		},
		/**判断是否授权**/
		jsAuthorizeAPI(AuthSetting, type) {
			uni.getSetting({
				success: setRes => {
					//scope.record:是否授权录音功能
					//scope.camera:是否授权摄像头
					if (setRes.authSetting[AuthSetting] != true) {
						uni.authorize({
							scope: AuthSetting,
							success: authRes => {
								// 权限开启成功
								this.authorizeSuccess(type);
							},
							fail: authErr => {
								//权限开启失败
								uni.showModal({
									title: '授权' + (type == 1 ? '摄像机' : '录音') + '失败',
									showCancel: false,
									content: '点击确定重试',
									success: modelRes => {
										if (modelRes.confirm) {
											this.openSettingAPI(AuthSetting, type);
										}
									}
								});
							}
						});
					} else {
						this.authorizeSuccess(type);
					}
				}
			});
		},
		/**打开授权设置**/
		openSettingAPI(AuthSetting, type) {
			uni.openSetting({
				success: openRes => {
					if (openRes.authSetting[AuthSetting] != true) {
						//权限开启失败
						uni.showModal({
							title: '授权' + (type == 1 ? '摄像机' : '录音') + '失败',
							showCancel: false,
							content: '点击确定重试',
							success: modelRes => {
								if (modelRes.confirm) {
									this.openSettingAPI(AuthSetting, type);
								}
							}
						});
					} else {
						// 权限开启成功
						this.authorizeSuccess(type);
					}
				}
			});
		},
		/**授权成功**/
		authorizeSuccess(type) {
			if (type == '1') {
				//摄像头授权成功,授权录音
				this.jsAuthorizeAPI('scope.record', 2);
			} else if (type == '2') {
				console.log('[打开摄像机]');
				this.getCameraAPI();
			}
		},
		/**拍摄视频**/
		getCameraAPI() {
			uni.showLoading({
				title: '打开摄像头',
				mask: true
			});
			//调取摄像机
			uni.chooseMedia({
				count: 1,
				mediaType: ['video'],
				sourceType: ['camera'],
				maxDuration: 5,
				camera: 'front',
				success: mediaRes => {
					console.log('[视频]', mediaRes);
					let filePath = mediaRes.tempFiles[0].tempFilePath;
					console.log('[视频数据]', filePath);
					//把视频转换成base64
					const fs = uni.getFileSystemManager();
					fs.readFile({
						filePath: filePath,
						encoding: 'base64',
						success: videoRes => {
							console.log('[转换成功]', videoRes);
							       //videoRes.data:转成base64格式的视频
							}
							});
						},
						fail: videoErr => {
							console.log('[转行失败]', videoErr);
							
						}
					});
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.face-wrap {
	margin: 12px auto;
	.face-from {
		border-radius: 8px;
		display: flex;
		flex-direction: column;
		.form-item {
			width: 91.47%;
			display: flex;
			flex-direction: column;
			margin: auto;
			margin-top: 20px;
			.form-item-title {
				font-size: 16px;
				font-family: Source Han Sans CN;
				font-weight: 400;
				color: #ffffff;
			}
			.form-item-con {
				border-bottom: 2px solid #70737f;
				height: 35px;
				display: flex;
				margin-top: 6px;
				.form-item-input {
					flex: 1;
					height: 100%;
					font-size: 14px;
					font-family: Source Han Sans CN;
					font-weight: 400;
					color: #ffffff;
				}
				.placeholderclass {
					color: #999999;
				}
			}
		}
	}
	.form-btn {
		width: 91.47%;
		height: 44px;
		background: linear-gradient(0deg, #f65555 0%, #e54848 100%);
		border-radius: 22px;
		font-size: 18px;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #fffefa;
		position: fixed;
		display: flex;
		justify-content: center;
		align-items: center;
		bottom: 25px;
		left: 4.265%;
	}
	.form-disabled {
		background: #1b202f;
		width: 91.47%;
		height: 44px;
		border-radius: 22px;
		font-size: 18px;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #82838f;
		position: fixed;
		display: flex;
		justify-content: center;
		align-items: center;
		bottom: 25px;
		left: 4.265%;
	}
}
</style>

腾讯云-人脸核身官网

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值