news 2026/5/12 14:13:57

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

作者头像

张小明

前端开发工程师

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/5/9 11:33:29

硬件研发周期变长怎么办?3 个跨部门协作方法让项目管理提速

硬件研发周期变长,往往不是单点效率问题,而是跨部门协作缺少共同节奏、共同事实与共同验收,导致等待与返工叠加。本文基于 IPD(集成式产品开发)体系,并结合其中常用的 阶段门/决策门(Stage-Gate…

作者头像 李华
网站建设 2026/5/7 23:46:28

偶信科技是干嘛的?——解码深蓝,让海洋“开口说话”

当人类仰望星空时,别忘了脚下还有另一片未知疆域——覆盖地球71%表面的浩瀚海洋。它深邃、流动、充满声响,却因水体的隔绝而难以被直接感知。如何穿透这片“液态迷雾”,获取真实、可靠、连续的海洋信息?这正是偶信科技自创立以来所…

作者头像 李华
网站建设 2026/5/8 1:21:28

LobeChat默认模型推荐列表:哪些开源LLM表现最出色?

LobeChat 与开源大模型的完美搭档:谁才是本地 AI 助手的最佳选择? 在如今这个“人人都想拥有自己的 AI 助手”的时代,一个直观、高效且安全的交互界面变得前所未有的重要。尽管像 ChatGPT 这样的闭源服务提供了强大的语言能力,但高…

作者头像 李华
网站建设 2026/5/11 12:10:23

简单理解:电机驱动板的种类有哪些?

从行业通用分类逻辑(按控制方式、功能定位、功率等级),电机驱动板的完整分类及核心区别如下,覆盖所有场景(不止你的 2804 无刷电机):一、按控制算法分类(核心维度)1. 六步…

作者头像 李华
网站建设 2026/5/11 14:34:05

用Miniconda实现Python 3.8与3.9共存

用 Miniconda 实现 Python 多版本共存:轻量级 AI 开发环境实战 你有没有遇到过这种场景?刚跑通一个基于 PyTorch Lightning 的实验,信心满满地想复现一篇新论文的代码,结果 requirements.txt 里写着“仅支持 Python ≥3.9”——而…

作者头像 李华
网站建设 2026/5/8 22:06:16

卷积神经网络基础:YOLO初学者必备知识

卷积神经网络基础:YOLO初学者必备知识 在智能摄像头自动识别行人、无人机实时追踪移动目标、工厂流水线自动检测产品缺陷的今天,背后支撑这些“看得见”的智能能力,往往离不开一个核心算法——YOLO(You Only Look Once&#xff09…

作者头像 李华