当前位置: 首页 > news >正文

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

一.背景

目前需要实现活体检测功能,而且是需要静默活体,但是现在官方的活体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) } }
http://www.gsyq.cn/news/110347.html

相关文章:

  • 2025年北京防伪溯源解决方案服务商排行榜,北京信飞智创科技 - mypinpai
  • 资源监控脚本
  • 2025年汽车出海外贸推广平台TOP5推荐(12月更新):Facebook、LinkedIn、TikTok、Google、INS等全平台覆盖 - 品牌2026
  • LobeChat能否生成邮件模板?商务沟通提速器
  • AI 落地之殇:通用 Agent 为何解决不了业务问题?Anthropic 给出了个“Skills”(技能)思路
  • 安全测试公开课来啦!带你掌握安全测试的核心实战技能
  • 功放数字预失真(DPD)算法研究及MATLAB实现
  • 从小白到高手:手把手教你玩转LobeChat所有核心功能
  • 2025年五大防窜货扫码软件、防窜货标签技术、防窜货监控方案 - myqiye
  • 杭州到石家庄、唐山、秦皇岛、邯郸、邢台、保定、张家口、承德、沧州、廊坊、衡水搬家公司搬家物流推荐!跨省搬家排行榜 - 物流人
  • 文字秒变流程图,工作效率大提升
  • linux配置开机jar包自启动
  • 2025年综合性能检测设备定做厂家权威推荐榜单:车辆检测线/汽车环保检测设备/安检线设备源头厂家精选 - 品牌推荐官
  • 2025年SDP硅烷弹性防腐涂料制造厂权威推荐榜单:防火涂料/钢结构防火涂料施工/耐高温防腐涂料源头制造厂精选 - 品牌推荐官
  • YOLOv5 vs YOLOv8:哪个更适合你的项目场景?
  • LobeChat能否实现AI绘画描述生成?Stable Diffusion联动
  • day41 早停策略和模型保存
  • LangChain代理(Agent)调用Anything-LLM执行外部工具操作
  • PaddlePaddle镜像加速指南:如何通过清华源快速下载并部署GPU环境
  • Git下载缓慢时使用镜像加速器的配置方法
  • Codex效率命令安全审查:防止Anything-LLM生成危险指令
  • 从研究到生产:TensorFlow全流程开发中的清华源应用场景
  • 空气加热器厂家TOP5权威推荐:甄选优质制造商,赋能工业加热 - 工业品牌热点
  • PaddlePaddle与Dify智能体平台集成:实现AI应用快速上线
  • 2025年12月三轴向振动试验台,震动试验台,垂直振动试验台公司推荐:行业测评与选择指南 - 品牌鉴赏师
  • 超级好用的U盘系统Winpe HotPE 2.8 纯净版:Win11 内核 + 全驱动,无广告无劫持装机 / 救急必备!
  • 澳洲天然保健品牌SK登陆中国 认证加持获市场青睐 - 资讯焦点
  • 国产AI框架PaddlePaddle镜像部署:集成cuda安装与maven下载优化
  • 网易有道开源情感语音合成引擎EmotiVoice
  • 利用少量数据训练出媲美真人发音的语音模型方法论