codecamp

鸿蒙OS 生物特征识别开发指导

场景介绍

当前生物特征识别支持 2D 人脸识别、3D 人脸识别,可应用于设备解锁、应用登录、支付等身份认证场景。

接口说明

BiometricAuthentication 类提供了生物认证的相关方法,包括检测认证能力、认证和取消认证等,用户可以通过人脸等生物特征信息进行认证操作。在执行认证前,需要检查设备是否支持该认证能力,具体指认证类型、安全级别和是否本地认证。如果不支持,需要考虑使用其他认证能力。

接口名 功能描述
getInstance(Ability ability) 获取 BiometricAuthentication的单例对象。
checkAuthenticationAvailability(AuthType type, SecureLevel level, boolean isLocalAuth) 检测设备是否具有生物认证能力。
execAuthenticationAction(AuthType type, SecureLevel level, boolean isLocalAuth,boolean isAppAuthDialog, SystemAuthDialogInfo information) 调用者使用该方法进行生物认证。可以使用自定义的认证界面,也可以使用系统提供的认证界面。当使用系统认证界面时,调用者可以自定义提示语。该方法直到认证结束才返回认证结果。
getAuthenticationTips() 获取生物认证过程中的提示信息。
cancelAuthenticationAction() 取消生物认证操作。
setSecureObjectSignature(Signature sign) 设置需要关联认证结果的Signature 对象,在进行认证操作后,如果认证成功则Signature 对象被授权可以使用。设置前 Signature 对象需要正确初始化,且配置为认证成功才能使用。
getSecureObjectSignature() 在认证成功后,可通过该方法获取已授权的 Signature 对象。如果未设置过 Signature 对象,则返回 null。
setSecureObjectCipher(Cipher cipher) 设置需要关联认证结果的 Cipher 对象,在进行认证操作后,如果认证成功则 Cipher 对象被授权可以使用。设置前 Cipher 对象需要正确初始化,且配置为认证成功才能使用。
getSecureObjectCipher() 在认证成功后,可通过该方法获取已授权的 Cipher 对象。如果未设置过 Cipher 对象,则返回 null。
setSecureObjectMac(Mac mac) 设置需要关联认证结果的 Mac 对象,在进行认证操作后,如果认证成功则 Mac 对象被授权可以使用。设置前 Mac 对象需要正确初始化,且配置为认证成功才能使用。
getSecureObjectMac() 在认证成功后,可通过该方法获取已授权的 Mac 对象。如果未设置过 Mac 对象,则返回 null。

开发步骤

开发前请完成以下准备工作:

  1. 在应用配置权限文件中,增加 ohos.permission.ACCESS_BIOMETRIC 的权限声明。
  2. 在使用生物特征识别认证能力的代码文件中增加 import ohos.biometrics.authentication.BiometricAuthentication。

开发过程:

  1. 获取 BiometricAuthentication 的单例对象,代码示例如下:

   BiometricAuthentication  mBiometricAuthentication = BiometricAuthentication.getInstance(MainAbility.mAbility);

  1. 检测设备是否具有生物认证能力:

2D 人脸识别建议使用 SECURE_LEVEL_S2,3D 人脸识别建议使用 SECURE_LEVEL_S3。代码示例如下:

   int retChkAuthAvb = mBiometricAuthentication.checkAuthenticationAvailability(
   BiometricAuthentication.AuthType.AUTH_TYPE_BIOMETRIC_FACE_ONLY, BiometricAuthentication.SecureLevel.SECURE_LEVEL_S2, true);

  1. (可选)设置需要关联认证结果的 Signature 对象或 Cipher 对象或 Mac 对象,代码示例如下:

   // 定义一个Signature对象sign;
   mBiometricAuthentication.setSecureObjectSignature(sign);

    
   // 定义一个Cipher对象cipher;
   mBiometricAuthentication.setSecureObjectCipher(cipher);

    
   // 定义一个Mac对象mac;
   mBiometricAuthentication.setSecureObjectMac(mac);

  1. 在新线程里面执行认证操作,避免阻塞其他操作,代码示例如下:

   new Thread(new Runnable() {
       @Override
       public void run() {
           int retExcAuth;
           retExcAuth = mBiometricAuthentication.execAuthenticationAction(        BiometricAuthentication.AuthType.AUTH_TYPE_BIOMETRIC_FACE_ONLY,        BiometricAuthentication.SecureLevel.SECURE_LEVEL_S2, true, false, null);
       }
   }).start();

  1. 获得认证过程中的提示信息,代码示例如下:

   AuthenticationTips  mTips = mBiometricAuthentication.getAuthenticationTips();

  1. (可选)认证成功后获取已设置的 Signature 对象或 Cipher 对象或 Mac 对象,代码示例如下:

   Signature  sign = mBiometricAuthentication.getSecureObjectSignature();

    
   Cipher cipher = mBiometricAuthentication.getSecureObjectCipher();

    
   Mac mac = mBiometricAuthentication.getSecureObjectMac();

  1. 认证过程中取消认证,代码示例如下:

   int  ret = mBiometricAuthentication.cancelAuthenticationAction();
鸿蒙OS 生物特征识别概述
鸿蒙OS 码生成
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

鸿蒙OS 开发

鸿蒙OS 术语

鸿蒙OS Java API参考

鸿蒙OS ohos.aafwk.ability

鸿蒙OS ohos.aafwk.abilityjet.activedata

鸿蒙OS ohos.aafwk.content

鸿蒙OS java.lang

鸿蒙OS java.Util

鸿蒙OS java.Util class

鸿蒙OS ohos.data.dataability

鸿蒙OS ohos.data.dataability class

鸿蒙OS ohos.agp.components

鸿蒙OS ohos.agp.components interface

鸿蒙OS ohos.agp.components class

鸿蒙OS ohos.global.configuration

鸿蒙OS java.io

鸿蒙OS ohos.data.resultset

鸿蒙OS ohos.data.resultset interface

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }