鸿蒙 NEXT开发中轻松实现人脸识别功能

简介: 在鸿蒙 NEXT 中,使用 CoreVision Kit 的 FaceDetector 模块可轻松实现人脸识别功能。本文介绍了从权限配置、图像获取到人脸检测的完整实现流程,并附上核心代码示例,助你快速集成该功能。

大家好,我是 V 哥。
今天给大家介绍在 HarmonyOS 原生鸿蒙开发中,实现人脸识别功能,这个功能在常用的 APP 开发中上镜率还是很高的,在传统的 Android 或 iOS 开发中,通常我们要借助第三方库来实现,而在鸿蒙原生开发中,天然的集成了这个功能,使用起来也超级方便,接下来听 V 哥细细说来。

在鸿蒙 NEXT 中实现人脸识别功能,可通过 CoreVision Kit 的 FaceDetector 模块实现。以下是基于 API 12+ 的实现流程及核心代码示例:


一、开发准备

  1. 配置权限
    在 module.json5 中添加以下权限:

    "requestPermissions": [
      {
          "name": "ohos.permission.READ_MEDIA" },       // 读取图片权限
      {
          "name": "ohos.permission.CAMERA" },          // 相机权限
      {
          "name": "ohos.permission.ACCESS_BIOMETRIC" } // 生物认证权限
    ]
    
  2. 导入依赖
    使用鸿蒙 NEXT 的视觉服务接口:

    import {
          faceDetector } from '@kit.CoreVisionKit';
    import image from '@kit.ImageKit'; // 图像处理模块
    

二、核心实现步骤

1. 初始化人脸检测器

// 初始化参数配置
let faceDetectConfig: faceDetector.FaceDetectOptions = {
   
  maxFaceNum: 5,         // 最大检测人脸数
  featureLevel: faceDetector.FeatureLevel.TYPE_FULL, // 检测全部特征
  algorithmMode: faceDetector.AlgorithmMode.TYPE_MODE_ACCURATE // 高精度模式
};

// 创建检测器实例
let detector = faceDetector.createFaceDetector(faceDetectConfig);

2. 获取图像数据
通过相机或相册获取图像,转换为 PixelMap 格式:

// 示例:从相册选择图片并转换为 PixelMap
async function getImagePixelMap() {
   
  let imageSource = image.createImageSource(selectedImageUri);
  let decodeOptions = {
   
    desiredSize: {
    width: 1024, height: 1024 } // 调整尺寸优化性能
  };
  let pixelMap = await imageSource.createPixelMap(decodeOptions);
  return pixelMap;
}

3. 执行人脸检测

async function detectFaces(pixelMap: image.PixelMap) {
   
  try {
   
    // 执行检测
    let faces: faceDetector.Face[] = await detector.detect(pixelMap);
    // 处理检测结果
    faces.forEach((face: faceDetector.Face) => {
   
      console.log("人脸置信度: " + face.confidence);
      console.log("人脸坐标框: " + JSON.stringify(face.rect));
      console.log("五官坐标点: " + JSON.stringify(face.landmarks));
    });
  } catch (error) {
   
    console.error("人脸检测失败: " + error.code + ", 信息: " + error.message);
  }
}

4. 身份验证集成

结合生物认证接口验证机主身份:

import userAuth from '@kit.BiometricAuthenticationKit';

// 检查是否支持人脸识别
let status = userAuth.getAvailableStatus(
  userAuth.UserAuthType.FACE, 
  userAuth.AuthTrustLevel.ATL3
);
if (status.isAvailable) {
   
  // 执行人脸认证
  userAuth.executeAuth(
    userAuth.UserAuthType.FACE,
    userAuth.AuthTrustLevel.ATL3,
    (err, result) => {
   
      if (result?.result === userAuth.AuthResult.SUCCESS) {
   
        console.log("身份验证通过");
      }
    }
  );
}

三、以下是一个完整示例代码

  1. Index.ets 主页面(示例代码是使用元服务项目实现)
import {
    authentication } from '@kit.AccountKit';
import {
    BusinessError } from '@kit.BasicServicesKit';
import {
    hilog } from '@kit.PerformanceAnalysisKit';

const DOMAIN = 0x0000;

export const pathStack: NavPathStack = new NavPathStack();

@Entry
@ComponentV2
struct Index {
   
  build() {
   
    Navigation(pathStack) {
   
      Column({
    space: 20 }) {
   
        Button("人脸活体检测-示例")
          .width("80%")
          .borderRadius(10)
          .onClick(() => {
   
            pathStack.pushPath({
    name: "live" })
          })
      }
      .height('100%')
      .width('100%')
    }
    .title("元服务")
  }

  aboutToAppear() {
   
    hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');
    this.loginWithHuaweiID();
  }

  /**
   * Sample code for using HUAWEI ID to log in to atomic service.
   * According to the Atomic Service Review Guide, when a atomic service has an account system,
   * the option to log in with a HUAWEI ID must be provided.
   * The following presets the atomic service to use the HUAWEI ID silent login function.
   * To enable the atomic service to log in successfully using the HUAWEI ID, please refer
   * to the HarmonyOS HUAWEI ID Access Guide to configure the client ID and fingerprint certificate.
   */
  private loginWithHuaweiID() {
   
    // Create a login request and set parameters
    const loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();
    // Whether to forcibly launch the HUAWEI ID login page when the user is not logged in with the HUAWEI ID
    loginRequest.forceLogin = false;
    // Execute login request
    const controller = new authentication.AuthenticationController();
    controller.executeRequest(loginRequest).then((data) => {
   
      const loginWithHuaweiIDResponse = data as authentication.LoginWithHuaweiIDResponse;
      const authCode = loginWithHuaweiIDResponse.data?.authorizationCode;
      // Send authCode to the backend in exchange for unionID, session

    }).catch((error: BusinessError) => {
   
      hilog.error(DOMAIN, 'testTag', 'error: %{public}s', JSON.stringify(error));
      if (error.code === authentication.AuthenticationErrorCode.ACCOUNT_NOT_LOGGED_IN) {
   
        // HUAWEI ID is not logged in, it is recommended to jump to the login guide page

      }
    });
  }
}
  1. LiveDetectPage.ets 页面

    import {
          common, abilityAccessCtrl, Permissions } from '@kit.AbilityKit';
    import {
          interactiveLiveness } from '@kit.VisionKit';
    import {
          BusinessError } from '@kit.BasicServicesKit';
    import {
          hilog } from '@kit.PerformanceAnalysisKit';
    @Builder export function buildPage(){
         
     LiveDetectPage()
    }
    @Component
    export struct LiveDetectPage {
         
     private context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
     // 权限
     private array: Array<Permissions> = ["ohos.permission.CAMERA"];
     // 动作个数
     @State actionsNum: number = 0;
     /**
      * 是否是静默模式
      * 静默模式(SILENT_MODE):表示静默活体检测模式,暂未支持。
      * 交互模式(INTERACTIVE_MODE):表示动作活体检测模式。
      */
     @State isSilentMode: string = "INTERACTIVE_MODE";
     // 验证完的跳转模式
     @State routeMode: string = "replace";
     // 验证结果
     @State resultInfo: interactiveLiveness.InteractiveLivenessResult = {
         
         livenessType: 0
     };
     // 验证失败结果
     @State failResult: Record<string, number | string> = {
         
         "code": 1008302000,
         "message": ""
     };
    
     build() {
         
         NavDestination() {
         
             // 层叠布局
             Stack({
         
                 // 内容对齐方式:顶部对齐
                 alignContent: Alignment.Top
             }) {
         
                 // 列容器组件
                 Column() {
         
                     // 行容器组件
                     Row() {
         
                         // 弹性布局:主轴方向为横向,内容对齐方式为起始对齐,垂直方向对齐方式为居中对齐
                         Flex({
          direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
         
                             // 文本显示组件
                             Text("验证完的跳转模式:").fontSize(18).width("25%")
                             // 弹性布局:主轴方向为横向,内容对齐方式为起始对齐,垂直方向居中对齐
                             Flex({
          direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
         
                                 // 行容器组件
                                 Row() {
         
                                     // 单选框组件
                                     Radio({
          value: "replace", group: "routeMode" }).checked(true).height(24).width(24)
                                         .onChange((isChecked: boolean) => {
         
                                             this.routeMode = "replace"
                                         })
                                     Text("replace").fontSize(16)
                                 }.margin({
          right: 15 })
    
                                 Row() {
         
                                     // 单选框组件
                                     Radio({
          value: "back", group: "routeMode" }).checked(false).height(24).width(24)
                                         .onChange((isChecked: boolean) => {
         
                                             this.routeMode = "back";
                                         })
                                     Text("back").fontSize(16)
                                 }
                             }.width("75%")
                         }
                     }.margin({
          bottom: 30 })
    
                     // 行容器组件
                     Row() {
         
                         // 弹性布局:主轴方向为横向,内容对齐方式为起始对齐,垂直方向对齐方式为居中对齐
                         Flex({
          direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
         
                             Text("动作数量:").fontSize(18).width("25%")
                             TextInput({
         
                                 placeholder: this.actionsNum != 0 ? this.actionsNum.toString() : "动作数量为3或4个"
                             }).type(InputType.Number).placeholderFont({
         
                                 size: 18,
                                 weight: FontWeight.Normal,
                                 family: "HarmonyHeiTi",
                                 style: FontStyle.Normal
                             }).fontSize(18).fontWeight(FontWeight.Bold).fontFamily("HarmonyHeiTi")
                                 .fontStyle(FontStyle.Normal).width("65%").onChange((value: string) => {
         
                                 this.actionsNum = Number(value) as interactiveLiveness.ActionsNumber;
                             })
                         }
                     }
                 }.margin({
          left: 24, top: 80 }).zIndex(1)
    
                 // 层叠布局组件:内容对齐方式为底部对齐
                 Stack({
          alignContent: Alignment.Bottom }) {
         
                     if (this.resultInfo?.mPixelMap) {
         
                         // 如果存在mPixelMap,则显示检测的图片
                         Image(this.resultInfo?.mPixelMap).width(260)
                             .height(260).align(Alignment.Center)
                             .margin({
          bottom: 260 })
                         // 圆形遮罩
                         Circle().width(300).height(300).fillOpacity(0)
                             .strokeWidth(60).stroke(Color.White)
                             .margin({
          bottom: 250, left: 0 })
                     }
    
                     // 判断检测成功还是失败
                     Text(this.resultInfo.mPixelMap ? "检测成功" : this.failResult.code != 1008302000 ? "检测失败" : "")
                         .width("100%").height(26).fontSize(20).fontColor("#000000").fontFamily("HarmonyHeiTi")
                         .margin({
          top: 50 }).textAlign(TextAlign.Center).fontWeight("Medium").margin({
          bottom: 240 })
    
                     // 如果检测失败,则显示检测失败的原因
                     if(this.failResult.code != 1008302000) {
         
                         Text(this.failResult.message as string)
                             .width("100%").height(26)
                             .textAlign(TextAlign.Center).fontFamily("HarmonyHeiTi").fontWeight("Medium")
                             .margin({
          bottom: 200 })
                     }
    
                     // 开始检测的按钮
                     Button("开始检测").type(ButtonType.Normal).borderRadius(10)
                         .width('60%').height(40).fontSize(16)
                         .margin({
          bottom: 56 })
                         .onClick(() => {
         
                             this.privateStartDetection();
                         })
                 }.height("100%")
             }
         }.title('活体检测')
         .onWillShow(() => {
         
             // 释放结果
             this.resultRelease();
             // 获取检测的结果
             this.getDetectionResultInfo();
         })
     }
    
     // 跳转到人脸活体检测控件
     private privateRouterLibrary() {
         
         // 交互式活体检测配置实例
         let routerOptions: interactiveLiveness.InteractiveLivenessConfig = {
         
             // 是否是静默模式
             isSilentMode: this.isSilentMode as interactiveLiveness.DetectionMode,
             // 路由模式:返回还是替换
             routeMode: this.routeMode as interactiveLiveness.RouteRedirectionMode,
             // 动作个数
             actionsNum: this.actionsNum
         }
    
         // 如果可以使用该能力(活体检测能力)
         // syscap配置
         if (canIUse("SystemCapability.AI.Component.LivenessDetect")) {
         
             // 开始活体检测
             interactiveLiveness.startLivenessDetection(routerOptions).then((DetectState: boolean) => {
         
                 // 如果检测成功,则跳转到下一个页面
                 hilog.info(0x0001, "LivenessCollectionIndex", `Succeeded in jumping.`);
             }).catch((err: BusinessError) => {
         
                 // 如果检测失败,则显示检测失败的原因
                 hilog.error(0x0001, "LivenessCollectionIndex", `Failed to jump. Code:${
           err.code},message:${
           err.message}`);
             })
         } else {
         
             // 如果不可以使用该能力(活体检测能力),则显示不支持该能力的提示
             hilog.error(0x0001, "LivenessCollectionIndex", '当前设备不支持活体检测API');
         }
     }
    
     /**
      * 返回从用户获取到的权限列表,遍历该列表,如果包含了ohos.permission.CAMERA权限,则调用privateRouterLibrary方法
      */
     private privateStartDetection() {
         
         abilityAccessCtrl.createAtManager().requestPermissionsFromUser(this.context, this.array).then((res) => {
         
             for (let i = 0; i < res.permissions.length; i++) {
         
                 if (res.permissions[i] === "ohos.permission.CAMERA" && res.authResults[i] === 0) {
         
                     this.privateRouterLibrary();
                 }
             }
         }).catch((err: BusinessError) => {
         
             hilog.error(0x0001, "LivenessCollectionIndex", `Failed to request permissions from user. Code is ${
           err.code}, message is ${
           err.message}`);
         })
     }
    
     /**
      * 获取检测的结果
      */
     private getDetectionResultInfo() {
         
         // getInteractiveLivenessResult接口调用完会释放资源
         // 如果可以使用活体检测能力(syscap配置)
         if (canIUse("SystemCapability.AI.Component.LivenessDetect")) {
         
             // 获取活体检测结果
             let resultInfo = interactiveLiveness.getInteractiveLivenessResult();
             resultInfo.then(data => {
         
                 // 获取到结果,则赋值
                 this.resultInfo = data;
             }).catch((err: BusinessError) => {
         
                 // 如果发生了异常,则设置失败结果的字段值
                 this.failResult = {
          "code": err.code, "message": err.message }
             })
         } else {
         
             // 当前设备不支持活体检测能力
             hilog.error(0x0001, "LivenessCollectionIndex", '该设备不支持活体检测API。');
         }
     }
    
     /**
      * 结果重置
      */
     private resultRelease() {
         
         this.resultInfo = {
         
             livenessType: 0
         }
         this.failResult = {
         
             "code": 1008302000,
             "message": ""
         }
     }
    }
    

注意,需要在module.json5中配置权限。

四、注意事项

  1. 真机调试
    需使用支持人脸识别的真机(如 P50 系列)测试,模拟器不支持)。
  2. 性能优化
    • 降低图像分辨率以提升检测速度。
    • 使用 AlgorithmMode.TYPE_MODE_FAST 快速模式平衡性能与精度。
  3. 特征比对
    进阶场景可通过 FaceComparator 比对两张人脸相似度。

五、扩展场景

  • 活体检测:通过 VisionKit 监测眨眼、转头动作。
  • 暗光增强:结合 ImageKit 调整图像亮度/对比度强化识别效果。

通过上述步骤,可快速实现基础人脸识别功能,并根据需求扩展至复杂场景。


六、小结一下

以上就是在鸿蒙 NEXT 开发中实现人脸识别功能,也称为活体检测,通过鸿蒙 NEXT 提供的能力,可以轻松实现,兄弟们,可以抓紧试试(请自备真机测试)。学习鸿蒙可以关注威哥写的《鸿蒙 HarmonyOS NEXT 开发之路》卷1,卷2已上市,卷3正在加紧印刷中。如果你还没有拿下鸿蒙认证,快到碗里来(https://developer.huawei.com/consumer/cn/training/classDetail/042cb1cc4d7d44ecbdbd902fd1275dcc)。

WX20250408-100927@2x.png

相关文章
|
数据管理 API Android开发
鸿蒙开发:一文了解桌面卡片
在鸿蒙开发中,服务卡片虽然功能丰富,但也存在一些限制。例如,它不支持极速预览、断点调试和Hot Reload热重载等功能,同时也无法使用setTimeOut。此外,开发过程中还面临其他约束,如不支持导入动态共享包、使用native语言开发或加载native so。目前,服务卡片仅支持基于ArkUI的开发方式,且不支持跨平台开发,仅能使用声明式范式的部分组件、事件、动效、数据管理、状态管理和API能力。
1032 3
鸿蒙开发:一文了解桌面卡片
|
Ubuntu 数据安全/隐私保护
Ubuntu的默认root密码是多少,修改root密码
Ubuntu的默认root密码是多少,修改root密码
1005 0
|
JavaScript 开发工具 Android开发
如何在原生 App 中调用 Uniapp 的页面?
如何在原生 App 中调用 Uniapp 的页面?
2385 138
|
移动开发 开发工具 Android开发
Uniapp与原生App集成的流程
Uniapp与原生App集成的流程
4263 156
|
人工智能 计算机视觉
HarmonyOS NEXT AI基础视觉服务-人脸识别
这是一个基于AI基础视觉服务的人脸识别案例,通过调用设备相册选择图片,利用MediaLibraryKit、ImageKit和CoreVisionKit等模块完成图像处理与人脸检测,并展示结构化结果。核心功能包括:相册访问授权、图像数据转换、人脸位置及特征点检测,最终以弹窗形式输出检测信息。代码涵盖模块导入、功能实现与UI构建,适合学习AI视觉应用开发流程。
|
Shell 测试技术 API
Claude Code 官方内部团队最佳实践!
Immerse,独立开发者、内容创作者、AGI实践者,分享编程、AI、开源等内容。关注公众号“沉浸式趣谈”及个人网站获取更新。欢迎点赞、评论、转发支持!本文介绍Claude Code——智能编程命令行工具及其使用技巧。
8414 0
|
Android开发 开发者
Android SVG动画详细例子
本文详细讲解了在Android中利用SVG实现动画效果的方法,通过具体例子帮助开发者更好地理解和应用SVG动画。文章首先展示了动画的实现效果,接着回顾了之前的文章链接及常见问题(如属性名大小写错误)。核心内容包括:1) 使用阿里图库获取SVG图形;2) 借助工具将SVG转换为VectorDrawable;3) 为每个路径添加动画绑定属性;4) 创建动画文件并关联SVG;5) 在ImageView中引用动画文件;6) 在Activity中启动动画。文末还提供了完整的代码示例和源码下载链接,方便读者实践操作。
732 65
|
安全 Java 编译器
JD-GUI,java反编译工具及原理: JavaDecompiler一个Java反编译器
Java Decompiler (JD-GUI) 是一款由 Pavel Kouznetsov 开发的图形化 Java 反编译工具,支持 Windows、Linux 和 Mac Os。它能将 `.class` 文件反编译为 Java 源代码,支持多文件标签浏览、高亮显示,并兼容 Java 5 及以上版本。JD-GUI 支持对整个 Jar 文件进行反编译,可跳转源码,适用于多种 JDK 和编译器。其原理基于将字节码转换为抽象语法树 (AST),再通过反编译生成代码。尽管程序可能带来安全风险,但可通过代码混淆降低可读性。最新版修复了多项识别错误并优化了内存管理。
11846 1
|
前端开发 开发工具 Android开发
小红书APP的全新鸿蒙NEXT端性能优化技术实践
从 2023 年开始,鸿蒙的优势愈发明显,已经成为可与 iOS、安卓媲美的第三大移动操作系统。从一些抖音视频中也可以看出,鸿蒙在流畅性方面甚至在某些层面上超过了 iOS。本次分享的主题是小红书在鸿蒙平台上的工程实践,主要聚焦于性能优化和探索。
1279 10