The face verification service provides an Android client SDK that you can use to add face authentication to your application. This topic uses code examples to demonstrate how to integrate the face verification service into your Android application.
If you have questions about API integration, API calls, or other issues related to Alibaba Cloud Visual Intelligence API, contact us through the DingTalk group (ID: 23109592).
Prerequisites
The application must run on a physical device with Android 4.4 or later.
Permissions
To support foldable phones, add the following permission to the application's AndroidManifest.xml file. This allows the SDK to listen for configuration changes when the device is folded or unfolded.
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>Get the SDK and demo code
Download the Android SDK
Download the Android Demo
Using an emulator poses a security risk. Running the application on an emulator is not supported.
Configure dependencies
Unzip the SDK and place all .aar packages in the libs folder.
In the project's build.gradle file, add the libs folder as a dependency repository.
repositories { flatDir { dirs 'libs' // The aar folder } }Add the configuration to the application's build.gradle file.
android { // Add the following content useLibrary 'org.apache.http.legacy' }Add the dependencies to the application's build.gradle file.
dependencies { .... // fastjson dependency implementation 'com.alibaba:fastjson:1.2.83_noneautotype' // Local dependencies compile(name: 'baseverify-2.3.32.1.250219103020', ext: 'aar') compile(name: 'facade-2.3.32.1.250219103020', ext: 'aar') compile(name: 'face-2.3.32.1.250219103020', ext: 'aar') compile(name: 'ocr-2.3.32.1.250219103020', ext: 'aar') compile(name: 'faceaudio-2.3.32.1.250219103020', ext: 'aar') compile(name: 'facelanguage-2.3.32.1.250219103020', ext: 'aar') compile(name: 'facephotinus-2.3.32.1.250219103020', ext: 'aar') compile(name: 'APSecuritySDK-deepSec-7.0.1.20240528.jiagu', ext: 'aar') }
Call the API
Initialize the SDK.
Initialize the SDK on the Android client to improve the identity verification experience and prepare data for face authentication. The following code provides an example:
ZIMFacade.install(context);Retrieve the metainfo data.
The following code provides an example:
String metaInfos = ZIMFacade.getMetaInfos(context);The client must pass this metainfo value when making a mobile client verification request.
Start authentication.
The following code provides an example:
ZIMFacade zimFacade = ZIMFacadeBuilder.create(MainActivity.this); HashMap<String, String> extParams = new HashMap<>(); // To specify the UI orientation for liveness detection (landscape), specify this item. // extParams.put(ZIMFacade.ZIM_EXT_PARAMS_KEY_SCREEN_ORIENTATION, ZIMFacade.ZIM_EXT_PARAMS_VAL_SCREEN_LAND); // To enable the return of a liveness detection video, specify this item and get the local path of the video from response.videoFilePath. extParams.put(ZIMFacade.ZIM_EXT_PARAMS_KEY_USE_VIDEO, ZIMFacade.ZIM_EXT_PARAMS_VAL_USE_VIDEO_TRUE); // To customize the color of the progress bar on the liveness detection page, set this item. This is optional. For example, use #FF0000 for red. extParams.put(ZIMFacade.ZIM_EXT_PARAMS_KEY_FACE_PROGRESS_COLOR, "#FF0000"); zimFacade.verify(verificationToken, true, extParams, new ZIMCallback() { @Override public boolean response(final ZIMResponse response) { if (1000 == response.code) { showMessageBox("Face authentication passed"); } else { showMessageBox("Face authentication failed ([" + response.code + "]" + response.reason + ")"); } return true; } });NoteThe `verificationToken` returned by the mobile client verification request API call is valid for 30 minutes. An error occurs if you perform face authentication after the token expires. You must complete the authentication within this validity period.
The `response` callback of `ZIMCallback` must return a value. The default value is true.
The output is returned.
The callback result from the face authentication SDK is in the `ZIMResponse` class. This class defines the result codes and their descriptions, as shown below:
/** * Callback result for face authentication. */ public class ZIMResponse { /** * Result code: * 1000: Face authentication successful. * 1001: System error. * 1003: Authentication interrupted. * 2002: Network error. * 2006: Face authentication failed. */ public int code; /** * Information about the reason for the result. */ public String reason; /** * Description of the result. This field can be empty. */ public String msg; /** * Device token. */ public String deviceToken; /** * If a video is returned, this field provides the path to the video. */ public String videoFilePath; /** * Photo from face verification. */ public byte[] bitmap; }Result descriptions.
HttpCode
Description
1000
Indicates that face authentication was successful. This result is for reference only. Call the mobile client verification query operation to get the final authentication result.
1001
Indicates a system error.
1003
Indicates that the authentication was interrupted.
2002
Indicates a network error.
2006
Indicates that face authentication failed. To get detailed failure reasons, call the mobile client verification query operation to get the final authentication result.
For more information, see Android Client Error Code Details.
API obfuscation configuration
To prevent class names from being obfuscated, refer to the following code:
-verbose
-keep class com.alipay.face.api.** {*;}
-keep class com.alipay.zoloz.toyger.**{*;}
-keep class com.dtf.face.api.** {*;}
-keep class com.dtf.face.ocr.verify.DTFOcrFacade { *; }
-keep class com.dtf.face.verify.** {*;}
-keep class com.dtf.face.network.model.** {*;}
-keep class com.dtf.face.network.APICallback {*;}
-keep class com.dtf.face.config.**{*;}
-keep class com.dtf.face.log.** {*;}
-keep class com.dtf.face.ui.widget.ToygerWebView {*;}
-keep class com.dtf.toyger.base.** {*;}
-keep class com.dtf.face.network.mpass.biz.model.** { *; }
-keep class faceverify.** { *; }
-keep class ocrverify.** { *; }
-keep class com.alipay.alipaysecuritysdk.** { *; }
-keep class com.alipay.deviceid.** { *; }
# FastJson Keep
-keep class com.alibaba.fastjson.** {*;}
-keep class org.json.** {*;}