当前位置: 首页 > 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.jsqmd.com/news/99655/

相关文章:

  • 2025一物一码加工厂TOP5权威推荐:防伪溯源解决方案甄选 - myqiye
  • 2025年高性价比空气加热器工厂推荐:空气加热器制造商及厂家 - 工业推荐榜
  • 2025年中国手竿选购指南:十大公认好竿背后的实力之选 - 品牌2026
  • 2025年北京防伪溯源解决方案服务商排行榜,北京信飞智创科技 - mypinpai
  • 2025年店铺起名公司联系方式汇总:全国主流服务机构官方联系通道与合作价值解析 - 品牌推荐
  • 常用细胞培养基科普
  • 资源监控脚本
  • 2025年汽车出海外贸推广平台TOP5推荐(12月更新):Facebook、LinkedIn、TikTok、Google、INS等全平台覆盖 - 品牌2026
  • LobeChat能否生成邮件模板?商务沟通提速器
  • 2025年鱼竿十大品牌名单:中国口碑好十大鱼竿 - 品牌2026
  • AI 落地之殇:通用 Agent 为何解决不了业务问题?Anthropic 给出了个“Skills”(技能)思路
  • 谁还能不知道计算机组成结构与缓存
  • 安全测试公开课来啦!带你掌握安全测试的核心实战技能
  • 从小白到高手:手把手教你玩转LobeChat所有核心功能
  • 功放数字预失真(DPD)算法研究及MATLAB实现
  • 腾讯HunyuanVideo-Foley开源本地部署指南
  • TMS SQL server连接查询恢复条码
  • 深圳到石家庄、唐山、秦皇岛、邯郸、邢台、保定、张家口、承德、沧州、廊坊、衡水搬家公司搬家物流推荐!跨省搬家排行榜 - 物流人
  • 2025年诚信的实验室反应釜企业推荐,看看哪家性价比高? - 工业推荐榜
  • 活动回顾丨阿里云AI原生应用开发实战营AI Agent 专场(上海站)回顾PPT下载
  • 不得了!这家诚信酶制剂公司太值得关注!
  • 精测赋能智领未来——西城微科电子秤PCBA方案开发解析
  • 2025年五大防窜货扫码软件、防窜货标签技术、防窜货监控方案 - myqiye
  • 杭州到石家庄、唐山、秦皇岛、邯郸、邢台、保定、张家口、承德、沧州、廊坊、衡水搬家公司搬家物流推荐!跨省搬家排行榜 - 物流人
  • 市面上酶制剂制造企业,你了解多少?
  • 文字秒变流程图,工作效率大提升
  • linux配置开机jar包自启动
  • 2025年综合性能检测设备定做厂家权威推荐榜单:车辆检测线/汽车环保检测设备/安检线设备源头厂家精选 - 品牌推荐官
  • 2025年SDP硅烷弹性防腐涂料制造厂权威推荐榜单:防火涂料/钢结构防火涂料施工/耐高温防腐涂料源头制造厂精选 - 品牌推荐官
  • YOLOv5 vs YOLOv8:哪个更适合你的项目场景?