news 2026/2/17 6:19:02

鸿蒙实现自定义类似活体检测功能

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
鸿蒙实现自定义类似活体检测功能

一.背景

目前需要实现活体检测功能,而且是需要静默活体,但是现在官方的活体API还不支持静默,第三方的SDK也不支持,现在自定义一个类似活体检测的功能,但是不会去检测是否活体,拿到照片以后去调用人脸识别

二.实现思路

先调用相机,将相机设置为前置摄像头,将当前相机内容使用XComponent实时渲染,启用定时拍照功能,设置定时器,时间到以后利用截图组件功能去做,然后拿到图片地址去进行人脸识别,不保存到本地

三.代码

这边只放了首页代码,其余代码会在包里,在文章后附带

使用的时候将包放到项目中,直接跳转或者引用xsComponent这个页面

/* * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import { CameraConstants } from './constants/CameraConstants'; import { CameraUtils } from './utils/CameraUtils'; import { abilityAccessCtrl, bundleManager, common, PermissionRequestResult, Permissions } from '@kit.AbilityKit'; import { BusinessError, emitter } from '@kit.BasicServicesKit'; import Logger from './utils/Logger'; import { TwiceReqPermissionButtonComponent } from './component/TwiceReqPermissionButtonComponent'; import { StackXComponent } from './component/StackXComponent'; import LoadingDialog from '@lyb/loading-dialog'; import { DynamicsRouter } from 'common'; @Builder export function xsIndexBuilder() { xsIndex() } @Entry @Component struct xsIndex { @Provide cameraPosition: number = 1; // 固定为前置摄像头 @Provide notHasPermission: boolean = true; @Provide surfaceId: string = ''; @Provide timerShooting: number = 3; // 固定3秒 @Provide captureTimer: number = 0; @Provide isVisibleTimerSet: boolean = false; @Provide isVisibleTimer: boolean = false; @Provide isVisibleCapture: boolean = true; @Provide captureClickFlag: number = 0; @State autoCaptureStarted: boolean = false; // 标记是否已开始自动拍照 private uiContext: UIContext = this.getUIContext(); private cameraUtils = new CameraUtils(this.uiContext); checkPermissions(permission: Permissions) { let atManager = abilityAccessCtrl.createAtManager(); let bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION); let tokenID = bundleInfo.appInfo.accessTokenId; let authResults = atManager.checkAccessTokenSync(tokenID, permission); return authResults === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED; } aboutToDisappear(): void { this.cameraUtils.releaseCamera(); } AppStorage.setOrCreate('cameraUtils', this.cameraUtils); let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); let context: Context = this.getUIContext().getHostContext() as common.UIAbilityContext; atManager.requestPermissionsFromUser(context, ['ohos.permission.CAMERA']) .then((data: PermissionRequestResult) => { if (data.authResults[0] === 0) { this.notHasPermission = false; this.cameraUtils.cameraShooting(this.cameraPosition, this.surfaceId, context); // 延迟启动自动拍照,等待相机初始化完成 setTimeout(() => { this.startAutoCapture(); }, 500); } else if (data.authResults[0] === -1) { this.notHasPermission = true; } }) .catch((err: BusinessError) => { Logger.error(`data: ${JSON.stringify(err)}`); }); } // 开始自动拍照流程 startAutoCapture(): void { if (this.autoCaptureStarted || this.notHasPermission) { return; } this.autoCaptureStarted = true; this.isVisibleTimer = true; this.captureTimer = this.timerShooting; this.isVisibleCapture = false; // 3秒后自动拍照 setTimeout(() => { this.cameraUtils.capture(true); // 前置摄像头需要镜像 this.captureClickFlag = this.captureClickFlag + 1; this.isVisibleCapture = true; this.isVisibleTimer = false; }, this.captureTimer * 1000); } onPageShow() { if (!this.notHasPermission) { let permissions: Permissions = 'ohos.permission.CAMERA'; if (this.checkPermissions(permissions)) { this.cameraUtils.cameraShooting(this.cameraPosition, this.surfaceId, this.getUIContext().getHostContext()!); this.notHasPermission = false; // 如果还没有开始自动拍照,则启动 if (!this.autoCaptureStarted) { setTimeout(() => { this.startAutoCapture(); }, 500); } } else { this.notHasPermission = true; } } } onPageHide(): void { if (!this.notHasPermission) { this.cameraUtils.releaseCamera(); } } build() { NavDestination() { Column() { if (this.notHasPermission) { TwiceReqPermissionButtonComponent(); } else { Row() { Image($r('app.media.back')) .width(25) .height(25) .onClick(() => { DynamicsRouter.pop() }) }.width('100%').padding(15) StackXComponent(); } } .height(CameraConstants.FULL_SCREEN) .width(CameraConstants.FULL_SCREEN) .backgroundColor(Color.White) // 白色背景 .padding({ top: this.uiContext.px2vp(AppStorage.get('topAvoid')) as number, bottom: this.uiContext.px2vp(AppStorage.get('bottomAvoid')) as number }); }.hideBackButton(true) } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/13 20:03:05

NVIDIA TensorRT-LLM大语言模型推理优化详解

NVIDIA TensorRT-LLM大语言模型推理优化详解 在当前生成式AI爆发的浪潮中,大语言模型(LLMs)已从实验室走向真实业务场景——智能客服、代码补全、内容创作等应用对响应速度和并发能力提出了前所未有的要求。一个70亿参数的模型如果用原始PyTo…

作者头像 李华
网站建设 2026/2/2 22:54:32

Ubuntu部署Xingrin(星环)企业级漏洞扫描与资产管理平台

平台概述与核心功能 一款现代化的企业级漏洞扫描与资产管理平台 提供自动化安全检测、资产发现、漏洞管理等功能 ✨ 功能特性 🎯 目标与资产管理 组织管理 - 多层级目标组织,灵活分组目标管理 - 支持域名、IP目标类型资产发现 - 子域名、网站…

作者头像 李华
网站建设 2026/2/9 17:09:15

VSCode Jupyter集成Anything-LLM实现智能问答

VSCode Jupyter集成Anything-LLM实现智能问答 在数据科学和工程实践中,最让人头疼的往往不是技术难题本身,而是那些“明明记得有文档提过”的细节问题。你正在写一段处理订单数据的代码,突然卡住了:这个 status 字段里的 "p…

作者头像 李华
网站建设 2026/2/10 13:09:11

飞桨Paddle 3.0部署DeepSeek-R1-Distill系列模型实践

飞桨Paddle 3.0部署DeepSeek-R1-Distill系列模型实践 在大模型落地日益迫切的今天,如何高效、稳定地将前沿语言模型部署到不同硬件平台,成为开发者面临的核心挑战之一。近期,飞桨(PaddlePaddle)发布了3.0版本&#xf…

作者头像 李华
网站建设 2026/2/14 17:38:23

LobeChat能否实现智能回复建议?IM工具增强插件构想

LobeChat能否实现智能回复建议?IM工具增强插件构想 在现代企业沟通场景中,信息洪流正以前所未有的速度冲击着团队的协作效率。每天成百上千条消息在IM工具中穿梭,员工不得不频繁切换上下文、反复敲打相似内容——尤其是在客服响应、项目跟进或…

作者头像 李华
网站建设 2026/2/14 21:47:29

OpenSpec兼容性列表新增TensorRT v8.6支持

OpenSpec 兼容性列表新增 TensorRT v8.6 支持 在当今 AI 应用密集落地的背景下,从云端大模型服务到边缘端智能设备,推理性能已成为决定系统成败的关键瓶颈。一个训练得再精准的模型,若在生产环境中响应迟缓、资源消耗过高,其商业价…

作者头像 李华