鴻蒙OS 生物特征識(shí)別開發(fā)指導(dǎo)

2020-09-18 14:51 更新

場(chǎng)景介紹

當(dāng)前生物特征識(shí)別支持 2D 人臉識(shí)別、3D 人臉識(shí)別,可應(yīng)用于設(shè)備解鎖、應(yīng)用登錄、支付等身份認(rèn)證場(chǎng)景。

接口說明

BiometricAuthentication 類提供了生物認(rèn)證的相關(guān)方法,包括檢測(cè)認(rèn)證能力、認(rèn)證和取消認(rèn)證等,用戶可以通過人臉等生物特征信息進(jìn)行認(rèn)證操作。在執(zhí)行認(rèn)證前,需要檢查設(shè)備是否支持該認(rèn)證能力,具體指認(rèn)證類型、安全級(jí)別和是否本地認(rèn)證。如果不支持,需要考慮使用其他認(rèn)證能力。

接口名 功能描述
getInstance(Ability ability) 獲取 BiometricAuthentication的單例對(duì)象。
checkAuthenticationAvailability(AuthType type, SecureLevel level, boolean isLocalAuth) 檢測(cè)設(shè)備是否具有生物認(rèn)證能力。
execAuthenticationAction(AuthType type, SecureLevel level, boolean isLocalAuth,boolean isAppAuthDialog, SystemAuthDialogInfo information) 調(diào)用者使用該方法進(jìn)行生物認(rèn)證。可以使用自定義的認(rèn)證界面,也可以使用系統(tǒng)提供的認(rèn)證界面。當(dāng)使用系統(tǒng)認(rèn)證界面時(shí),調(diào)用者可以自定義提示語。該方法直到認(rèn)證結(jié)束才返回認(rèn)證結(jié)果。
getAuthenticationTips() 獲取生物認(rèn)證過程中的提示信息。
cancelAuthenticationAction() 取消生物認(rèn)證操作。
setSecureObjectSignature(Signature sign) 設(shè)置需要關(guān)聯(lián)認(rèn)證結(jié)果的Signature 對(duì)象,在進(jìn)行認(rèn)證操作后,如果認(rèn)證成功則Signature 對(duì)象被授權(quán)可以使用。設(shè)置前 Signature 對(duì)象需要正確初始化,且配置為認(rèn)證成功才能使用。
getSecureObjectSignature() 在認(rèn)證成功后,可通過該方法獲取已授權(quán)的 Signature 對(duì)象。如果未設(shè)置過 Signature 對(duì)象,則返回 null。
setSecureObjectCipher(Cipher cipher) 設(shè)置需要關(guān)聯(lián)認(rèn)證結(jié)果的 Cipher 對(duì)象,在進(jìn)行認(rèn)證操作后,如果認(rèn)證成功則 Cipher 對(duì)象被授權(quán)可以使用。設(shè)置前 Cipher 對(duì)象需要正確初始化,且配置為認(rèn)證成功才能使用。
getSecureObjectCipher() 在認(rèn)證成功后,可通過該方法獲取已授權(quán)的 Cipher 對(duì)象。如果未設(shè)置過 Cipher 對(duì)象,則返回 null。
setSecureObjectMac(Mac mac) 設(shè)置需要關(guān)聯(lián)認(rèn)證結(jié)果的 Mac 對(duì)象,在進(jìn)行認(rèn)證操作后,如果認(rèn)證成功則 Mac 對(duì)象被授權(quán)可以使用。設(shè)置前 Mac 對(duì)象需要正確初始化,且配置為認(rèn)證成功才能使用。
getSecureObjectMac() 在認(rèn)證成功后,可通過該方法獲取已授權(quán)的 Mac 對(duì)象。如果未設(shè)置過 Mac 對(duì)象,則返回 null。

開發(fā)步驟

開發(fā)前請(qǐng)完成以下準(zhǔn)備工作:

  1. 在應(yīng)用配置權(quán)限文件中,增加 ohos.permission.ACCESS_BIOMETRIC 的權(quán)限聲明。
  2. 在使用生物特征識(shí)別認(rèn)證能力的代碼文件中增加 import ohos.biometrics.authentication.BiometricAuthentication。

開發(fā)過程:

  1. 獲取 BiometricAuthentication 的單例對(duì)象,代碼示例如下:

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

  1. 檢測(cè)設(shè)備是否具有生物認(rèn)證能力:

2D 人臉識(shí)別建議使用 SECURE_LEVEL_S2,3D 人臉識(shí)別建議使用 SECURE_LEVEL_S3。代碼示例如下:

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

  1. (可選)設(shè)置需要關(guān)聯(lián)認(rèn)證結(jié)果的 Signature 對(duì)象或 Cipher 對(duì)象或 Mac 對(duì)象,代碼示例如下:

   // 定義一個(gè)Signature對(duì)象sign;
   mBiometricAuthentication.setSecureObjectSignature(sign);

    
   // 定義一個(gè)Cipher對(duì)象cipher;
   mBiometricAuthentication.setSecureObjectCipher(cipher);

    
   // 定義一個(gè)Mac對(duì)象mac;
   mBiometricAuthentication.setSecureObjectMac(mac);

  1. 在新線程里面執(zhí)行認(rèn)證操作,避免阻塞其他操作,代碼示例如下:

   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. 獲得認(rèn)證過程中的提示信息,代碼示例如下:

   AuthenticationTips  mTips = mBiometricAuthentication.getAuthenticationTips();

  1. (可選)認(rèn)證成功后獲取已設(shè)置的 Signature 對(duì)象或 Cipher 對(duì)象或 Mac 對(duì)象,代碼示例如下:

   Signature  sign = mBiometricAuthentication.getSecureObjectSignature();

    
   Cipher cipher = mBiometricAuthentication.getSecureObjectCipher();

    
   Mac mac = mBiometricAuthentication.getSecureObjectMac();

  1. 認(rèn)證過程中取消認(rèn)證,代碼示例如下:

   int  ret = mBiometricAuthentication.cancelAuthenticationAction();
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)