使用uniapp开发微信小程序的人脸采集功能/人脸识别功能

请添加图片描述

✅作者简介:大家好我是瓜子三百克,励志成为全栈工程师的一枚程序猿,也是喜欢在学习和开发中记录笔记的小白博主!
📃个人主页:瓜子三百克的主页
🔥系列专栏:uniapp前端
💖如果觉得博主的文章还不错的话,请点赞👍+收藏⭐️+留言📝支持一下博主哦🤞

请添加图片描述
本篇文章是借助微信小程序平台,添加人脸采集的功能,你看上面的漂亮姐姐有什么理由不去采集一下她的人脸呢。😂😂

开发环境:
开发框架:uniapp
开发平台:微信小程序


标签和样式先上:
标签:

<template>
	<view class="page-content">
		<view class="containerV">

			<view class="headerV">
				<view class="top-tips1">
					<view>请将正对手机,头部匹配摄像区域</view>
				</view>
				<view class="top-tips2">
					为了捍卫你的不要脸,请拍摄本人头像
				</view>
			</view>

			<view class="contentV">
				<view class="mark"></view>
				<image v-if="tempImg" mode="widthFix" :src="tempImg" />
				<camera v-if='isAuthCamera' :device-position="devicePosition ?'front': 'back'" class="camera"
					flash="off" resolution='high' />
				<view v-show="!tempImg && tipsText" class="tipV">{{ tipsText }}</view>
			</view>

			<view class="footerV">
				<view style="width: 100%;">
					<view v-if="!tempImg" style="width: 100%;">
						<view class="privacyV">
							<view class="icon"></view>
							<view class="text">
								照片隐私<text @click="handleJumpSecurityClick">安全保障</text>中…
							</view>
						</view>
						<view class="bottom-tips-2">该照片作为你不要脸的铁证</view>
						<view class="take-photo-bgV">
							<!-- 图片上传 -->
							<view v-show="true" class="btn-change-upload" @click="handleChooseImage" />
							<!--拍照-->
							<view class="btn-take-photo" @click="handleTakePhotoClick" />
							<!-- 切换镜头 -->
							<view class="btn-change-camera" @click="handleChangeCameraClick" />
						</view>
					</view>
					<view class="confirmV" v-else>
						<view class="btn-cancel" @click="handleCancelClick">
							取消
						</view>
						<view class="btn-ok" @click="handleOkClick">
							确定
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

样式:

<style lang="scss" scoped>
	.page-content {
		width: 100%;
		height: 100%;

		.containerV {
			width: 100%;
			height: 100%;

			.headerV {
				.top-tips1 {
					margin-top: 60rpx;
					color: #1C1C1C;
					font-size: 36rpx;
					text-align: center;
				}

				.top-tips2 {
					margin-top: 20rpx;
					color: #00AAFF;
					font-size: 28rpx;
					text-align: center;

				}
			}

			.contentV {
				position: relative;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;

				height: 661rpx;
				margin-top: 30rpx;

				.tipV {
					bottom: 30rpx;
					position: absolute;
					line-height: 90rpx;
					padding-left: 24rpx;
					padding-right: 24rpx;
					max-width: calc(100vw - 50rpx * 2);
					text-align: center;
					font-size: 30rpx;
					background: #000000;
					opacity: 0.75;
					color: #FFFFFF;
					border-radius: 16rpx;

					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					z-index: 5;
				}

				.camera {
					width: 100%;
					height: 100%;
				}

				.mark {
					position: absolute;
					left: 0;
					top: 0;
					z-index: 2;
					width: 750rpx;
					height: 100%;
					background: url("@/static/face/view_face_background.png") no-repeat center bottom;
					background-size: 750rpx 661rpx;
				}

				image {
					position: absolute;
					width: 100%;
					height: 100%;
					z-index: 3;
				}
			}

			.footerV {
				width: 100%;

				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;


				.privacyV {
					padding-top: 30rpx;

					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: center;

					.text {
						font-size: 30rpx;
						color: #1C1C1C;
						text-align: center;
						line-height: 42rpx;
						margin-left: 15rpx;

						text {
							font-size: 30rpx;
							color: #00AAFF;
							text-align: center;
							line-height: 42rpx;
						}
					}

					.icon {
						width: 40rpx;
						height: 47rpx;
						background: url("@/static/face/icon_face_security.png") no-repeat;
						background-size: 100% auto;
					}

				}

				.bottom-tips-2 {
					margin-top: 20rpx;
					color: #999999;
					text-align: center;
					font-size: 26rpx;
				}

				.take-photo-bgV {
					width: 100%;
					margin-top: 30rpx;

					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: center;


					.btn-take-photo {
						// 由于左边没有按钮,所以左边要便宜更大,以便是拍照按钮居中
						margin: 0rpx 80rpx 0rpx 80rpx;
						width: 196rpx;
						height: 196rpx;
						background: url("https://pro-file-qn.ztjy61.com/1003020211103145058685NNR9vlTm.png") no-repeat;
						background-size: 100% auto;
					}

					.btn-change-upload {
						left: 130rpx;
						width: 80rpx;
						height: 80rpx;
						background: url("@/static/face/icon_face_upload_picture.png") no-repeat;
						background-size: 100% auto;
					}

					.btn-change-camera {
						right: 130rpx;
						width: 80rpx;
						height: 80rpx;
						background: url("@/static/face/icon_face_switch_cameras.png") no-repeat;
						background-size: 100% auto;
					}
				}

				.confirmV {
					margin: 200rpx 100rpx 0rpx 100rpx;
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: space-between;

					.btn-cancel {
						font-size: 32rpx;
						color: #1C1C1C;
					}

					.btn-ok {
						font-size: 32rpx;
						color: #00AAFF;
					}
				}
			}
		}
	}
</style>

js逻辑:
关键点代码中已经注释


<script>
	export default {
		name: 'index',
		components: {

		},
		data() {
			return {
				tipsText: '', // 错误文案提示
				tempImg: '', // 本地图片路径

				cameraEngine: null, // 相机引擎
				devicePosition: false, // 摄像头朝向
				isAuthCamera: true, // 是否拥有相机权限
			}
		},

		onLoad(options) {
			this.initData()
		},

		methods: {
			// 初始化相机引擎
			initData() {
				// #ifdef MP-WEIXIN

				// 1、初始化人脸识别
				wx.initFaceDetect()
				// 2、创建 camera 上下文 CameraContext 对象
				this.cameraEngine = wx.createCameraContext()
				// 3、获取 Camera 实时帧数据
				const listener = this.cameraEngine.onCameraFrame((frame) => {
					if (this.tempImg) {
						return;
					}
					// 4、人脸识别,使用前需要通过 wx.initFaceDetect 进行一次初始化,推荐使用相机接口返回的帧数据
					wx.faceDetect({
						frameBuffer: frame.data,
						width: frame.width,
						height: frame.height,
						enablePoint: true,
						enableConf: true,
						enableAngle: true,
						enableMultiFace: true,
						success: (faceData) => {
							let face = faceData.faceInfo[0]
							if (faceData.x == -1 || faceData.y == -1) {
								this.tipsText = '检测不到人'
							}
							if (faceData.faceInfo.length > 1) {
								this.tipsText = '请保证只有一个人'
							} else {
								const {
									pitch,
									roll,
									yaw
								} = face.angleArray;
								const standard = 0.5
								if (Math.abs(pitch) >= standard || Math.abs(roll) >= standard ||
									Math.abs(yaw) >= standard) {
									this.tipsText = '请平视摄像头'
								} else if (face.confArray.global <= 0.8 || face.confArray.leftEye <=
									0.8 || face.confArray.mouth <= 0.8 || face.confArray.nose <= 0.8 ||
									face.confArray.rightEye <= 0.8) {
									this.tipsText = '请勿遮挡五官'
								} else {
									this.tipsText = '请拍照'
									// 这里可以写自己的逻辑了
								}
							}
						},
						fail: (err) => {
							if (err.x == -1 || err.y == -1) {
								this.tipsText = '检测不到人'
							} else {
								this.tipsText = err.errMsg || '网络错误,请退出页面重试'
							}
						},
					})
				})
				// 5、开始监听帧数据
				listener.start()
				// #endif
			},

			// 切换设备镜头
			handleChangeCameraClick() {
				this.devicePosition = !this.devicePosition;
			},

			// 图片上传
			handleChooseImage() {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album'],
					success: (res) => {
						if (res.errMsg === 'chooseImage:ok') {
							uni.showLoading({
								title: '照片上传中...'
							})
							console.log("===========:", res.tempFilePaths[0])
							this.handleOkClick()
						}
					},
					fail: (res) => {

					},
				});
			},

			// 拍照点击
			handleTakePhotoClick() {
				if (this.tipsText != "" && this.tipsText != "请拍照") {
					return;
				}

				uni.getSetting({
					success: (res) => {
						if (!res.authSetting['scope.camera']) {
							this.isAuthCamera = false
							uni.openSetting({
								success: (res) => {
									if (res.authSetting['scope.camera']) {
										this.isAuthCamera = true;
									}
								}
							})
						}
					}
				})
				this.cameraEngine.takePhoto({
					quality: "high",
					success: ({
						tempImagePath
					}) => {
						this.tempImg = tempImagePath
						console.log("=======tempImg:", this.tempImg)
					}
				})
			},

			// 点击确定上传
			handleOkClick() {
				uni.showLoading({
					mask: true,
					title: '校验中...'
				})
				// 更新人脸识别图片请求协议:传七牛图片链接				
				setTimeout(function() {
					uni.hideLoading()
					uni.showToast({
						icon: "none",
						title: "假装图片上传成功",
						duration: 2000,
					})
				}, 3000);
			},

			// 点击 - 取消上传
			handleCancelClick() {
				this.tempImg = ''
			},

			// 点击 - 人脸安全保障按钮
			handleJumpSecurityClick() {
				uni.showToast({
					icon: "none",
					title: "假装跳转人脸安全保障",
					duration: 2000,
				})
			},

		}
	}
</script>

拓展:
可以将图片上传至服务器,由服务器做人脸识别,这样功能就齐全了。

视频为上:

人脸采集

demo:
1、gitee:https://gitee.com/chenzm_186/face-detect-mini
2、csdn:https://download.csdn.net/download/weixin_38633659/85385944


**🏆结束语🏆 **

最后如果觉得我写的文章对您有帮助的话,欢迎点赞✌,收藏✌,加关注✌哦,谢谢谢谢!!

在这里插入图片描述

  • 48
    点赞
  • 125
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 85
    评论
### 回答1: uniapp是一款跨平台的开发框架,可以方便地开发出适配多个平台的应用程序。在uniapp使用微信小程序的API调用人脸识别功能也是可以实现的。 要在uniapp使用微信小程序人脸识别功能,首先需要在uniapp的项目配置文件中引入微信小程序的插件,具体步骤如下: 1. 在uniapp的项目目录中找到`manifest.json`文件,打开编辑。 2. 在`"mp-weixin"`字段下的`"usingComponents"`中添加以下内容: ``` "wx-open-data": "/static/wxopen-data", "tmpl": "/static/tmpl", "face": "/static/face" ``` 3. 在`"mp-weixin"`字段下的`"plugins"`数组中,添加以下内容: ``` { "name": "wx2f2c58f8b060adb1", "version": "1.1.3", "provider": "wx2f8bd3e2d38b0951" } ``` 4. 在uniapp的项目根目录下创建一个`static`文件夹,然后在该文件夹下再创建一个`wxopen-data`文件夹。 5. 将微信小程序SDK中的`wxopen-data`文件夹拷贝到上一步创建的`static/wxopen-data`文件夹下。 完成以上配置后,就可以在uniapp使用微信小程序人脸识别功能了。通过在uniapp的页面中调用相应的API,可以实现人脸识别功能,如获取用户的人脸信息,进行人脸比对等操作。 需要注意的是,由于uniapp是跨平台的开发框架,所以在使用微信小程序人脸识别功能时,需要保证运行环境是微信小程序,其他平台可能无法正常使用功能。 ### 回答2: UniApp是一个跨平台的开发框架,可以用于开发微信小程序。而微信小程序有提供人脸识别的能力。因此,我们可以使用UniApp来调用微信小程序人脸识别功能。 具体来说,我们可以在UniApp中引入微信小程序人脸识别相关的API,并在需要的时候调用这些API来实现人脸识别功能。首先,在UniApp的项目配置文件中,我们需要将微信小程序的AppID配置好。然后,在UniApp的页面文件中,我们可以使用uni.login()方法获取用户的登录凭证。接着,我们可以使用uni.checkSession()方法来检查用户登录状态。如果登录状态有效,我们可以通过uni.getUserInfo()方法获取用户的基本信息,包括头像和昵称等。然后,我们就可以使用微信小程序人脸识别API来进行人脸识别了。比如,我们可以使用uni.chooseImage()方法选择一张图片,然后使用uni.uploadFile()方法将图片上传到微信小程序的服务器。接着,我们可以使用微信小程序人脸识别API对图片进行人脸识别,得到人脸的相关信息。最后,我们可以将人脸识别的结果显示在UniApp的页面上,或者做相应的业务处理。 需要注意的是,为了使用微信小程序人脸识别功能,我们需要先在微信开放平台注册并申请相关的接口权限。同时,开发过程中也需要遵循微信小程序开发规范和限制。以上就是使用UniApp调用微信小程序人脸识别的简要介绍。
评论 85
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瓜子三百克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值