Read the original article:Face Liveness Detection in HarmonyOS: A VisionKit Implementation Guide
Introduction
Hello everyone! In this article, I’ll talk about Huawei’s HarmonyOS NEXT. It comes with VisionKit, a powerful tool that lets developers use AI-powered visual features. Here, I’ll focus on the Face Liveness Detection (interactiveLiveness) feature and explain how to integrate it into an app with examples.
📌 What is Face Liveness Detection?
Face Liveness Detection is a technology used to distinguish real human faces from fake ones (like photos, videos, or masks) through a device’s camera. In HarmonyOS NEXT, this feature verifies authenticity by asking users to perform interactive actions, such as blinking or nodding their head.
🎯 Use Cases
✅ Attendance tracking systems
✅ Secure app logins (identity verification)
✅ Fake user detection
✅ ID verification support
🧑💻 Development Steps
1. Add Required Module & Permissions
import { interactiveLiveness } from '@kit.VisionKit';
import { common, abilityAccessCtrl, Permissions } from '@kit.AbilityKit';
Add camera permission to your module.json5
file.
"requestPermissions":[
{
"name": "ohos.permission.CAMERA",
"reason": "$string:camera_desc",
"usedScene": {"abilities": []}
}
]
2. Design the User Interface
@State routeMode: string = "replace";
@State actionsNum: number = 3;
Radio({ value: "replace", group: "routeMode" }).checked(true)
Radio({ value: "back", group: "routeMode" }).checked(false)
TextInput({ placeholder: "Enter 3 or 4" }).type(InputType.Number)
3. Add a Start Button
Button("Start detection")
.onClick(() => {
this.startDetection();
})
4. Handle Permissions & Setup
private startDetection() {
abilityAccessCtrl.createAtManager().requestPermissionsFromUser(this.context, this.array).then((res) => {
if (res.permissions[0] === "ohos.permission.CAMERA" && res.authResults[0] === 0) {
this.runLivenessDetection();
}
})
}
5. Start Liveness Detection
private runLivenessDetection() {
let config = {
isSilentMode: "INTERACTIVE_MODE",
routeMode: this.routeMode,
actionsNum: this.actionsNum
}
interactiveLiveness.startLivenessDetection(config).then((success) => {
if (success) {
this.getDetectionResult();
}
}).catch((err) => {
console.error("Detection failed", err);
});
}
6. Get the Result
private getDetectionResult() {
interactiveLiveness.getInteractiveLivenessResult().then(result => {
console.log("Liveness type:", result.livenessType);
}).catch(err => {
console.error("Failed to get result:", err);
});
}
🛠️ About the APIs
The interactiveLiveness
module provides the following key functionalities for implementing face liveness detection:
⚙️startLivenessDetection(config: InteractiveLivenessConfig): Promise
Launches the face liveness detection screen using the provided configuration. The result of the redirection (success or failure) is returned as a Promise.
⚙️getInteractiveLivenessResult(): Promise
Retrieves the result of the liveness detection asynchronously. The returned result includes the detection mode, the best image captured, and optionally a secured buffer and certificate chain (for image security verification scenarios).
⚙️InteractiveLivenessConfig
A configuration object that allows you to customize the liveness detection behavior. It includes parameters such as:
-
isSilentMode
: Defines the detection mode. OnlyINTERACTIVE_MODE
is currently supported. -
actionsNum
: Specifies how many user actions (like blinking, nodding, etc.) will be randomly selected for detection. Only 3 or 4 actions are supported. -
routeMode
: Determines how the app navigates after detection, using eitherREPLACE_MODE
orBACK_MODE
. -
successfulRouteUrl
/failedRouteUrl
: Optional paths to navigate after success or failure. - Additional options such as
challenge
(for security scenarios),speechSwitch
(voice guidance), andisPrivacyMode
(requires permission).
⚙️Enum Types for Configuration:
-
DetectionMode
: Defines the detection method. Currently, onlyINTERACTIVE_MODE
is available. -
ActionsNumber
: Specifies the number of required actions (3 or 4). -
RouteRedirectionMode
: Controls routing behavior after detection (BACK_MODE
orREPLACE_MODE
).
📌 For full details, check the official API documentation.
🎉 Conclusion
The interactiveLiveness feature offered by VisionKit provides significant advantages for user verification and preventing fake face attacks in your HarmonyOS applications. It’s easy to use, runs on-device, and responds very quickly according to device performance.
If you’re developing a security-focused application or just want to add a user verification layer, this feature is perfect for you.
Top comments (0)