当前通过https://developer.huawei.com/consumer/cn/doc/harmonyos-references/vision-interactive-liveness#startlivenessdetection()方法跳转到人脸活体检测页面,通过getInteractiveLivenessResult()方法可以获取人脸活体检测结果,但无法确定何时调用该方法以获取采集结果。
人脸活体检测通常需要快速响应,在检测过程中需要实时获取结果并处理相关事件。目前官网文档的API提供跳转检测页面https://developer.huawei.com/consumer/cn/doc/harmonyos-references/vision-interactive-liveness#startlivenessdetection的方法和主动获取检测结果getInteractiveLivenessResult的功能。主动获取检测结果具体使用参考代码如下:
import { interactiveLiveness } from '@kit.VisionKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let successResult = interactiveLiveness.getInteractiveLivenessResult();
successResult.then(data => {
hilog.info(0x0001, 'LivenessCollectionIndex', `Succeeded in detecting.`);
}).catch((err: BusinessError) => {
hilog.error(0x0001, 'LivenessCollectionIndex', `Failed to detect. Code:${err.code},message:${err.message}`);
}); 人脸活体检测相对应的页面生命周期状态可参考下图逻辑:

检测开始:页面A点击开始检测按钮调用https://developer.huawei.com/consumer/cn/doc/harmonyos-references/vision-interactive-liveness#startlivenessdetection(),进入检测页面B,此时页面A进入onPageHide()阶段,页面B进入onPageShow()阶段。
检测结束:根据routeMode设置的不同值,在检测结束后会跳转至上一页面A或者新的页面C,此时会触发页面A或者页面C的onPageShow()阶段。
获取人脸活体检测结果可通过以下方案解决:根据routeMode设置的不同值,可以配置检测完成后路由跳转模式为back模式或者replace模式。
Q:注释“getInteractiveLivenessResult接口调用完会释放资源”是什么意思?
A:用户活体检测完成之后,调用getInteractiveLivenessResult接口会获取到上次活体检测的结果,接口返回结果之后会清理活体检测的结果,如果再次获取会显示算法在初始化。使用https://developer.huawei.com/consumer/cn/doc/harmonyos-references/vision-interactive-liveness#startlivenessdetection的callback获取活体检测的结果,接口返回结果之后会清理活体检测的结果。