HarmonyOS 人脸活体识别调用方法后没有任何反应

按照指南https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/vision-interactiveliveness-V5中的方法调用。

// 校验CAMERA权限
privatestartDetection() {
  let permissions: Array<Permissions> = [ 'ohos.permission.CAMERA'];
  let context: common.UIAbilityContext = getContext() as common.UIAbilityContext;
  abilityAccessCtrl.createAtManager().requestPermissionsFromUser(context, permissions).then((res) => {
    for (let i = 0; i < res.permissions.length; i++) {
      if (res.permissions[i] === "ohos.permission.CAMERA" && res.authResults[i] === 0) {
        this.privaterouterLibrary();
      }
    }
  })
}

private privaterouterLibrary() {

  PiccLog.info("privaterouterLibrary start");

  interactiveLiveness.startLivenessDetection({isSilentMode:interactiveLiveness.DetectionMode.SILENT_MODE,
    routeMode:interactiveLiveness.RouteRedirectionMode.REPLACE_MODE, actionsNum:2})
    .then((detectState: boolean) => {
      PiccLog.info("LivenessCollectionIndex" +  `Succeeded in jumping.`);
    }).catch((err: BusinessError) => {
    PiccLog.error( "LivenessCollectionIndex" + `Failed to jump. Code:${err.code},message:${err.message}`);
  })
}
}

授权成功,interactiveLiveness.startLivenessDetection这个方法执行后,页面和回调方法都没有任何反应。

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

startLivenessDetection传参格式有误,需要传的是两种模式的值, 参考代码如下:

import { common, abilityAccessCtrl, Permissions } from '@kit.AbilityKit';
import { interactiveLiveness } from '@kit.VisionKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

@Entry
@Component
struct LivenessIndex {
  private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
  private array: Array<Permissions> = ["ohos.permission.CAMERA"];
  @State actionsNum: number = 0;
  @State isSilentMode: string = "INTERACTIVE_MODE";
  @State routeMode: string = "replace";
  @State resultInfo: interactiveLiveness.InteractiveLivenessResult = {
    livenessType: 0
  };
  @State failResult: Record<string, number | string> = {
    "code": 1008302000,
    "message": ""
  };

  build() {
    Stack({
      alignContent: Alignment.Top
    }) {
      Column() {
        Row() {
          Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
            Text("选择模式:")
              .fontSize(18)
              .width("25%")
            Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
              Row() {
                Radio({ value: "INTERACTIVE_MODE", group: "isSilentMode" }).checked(true)
                  .height(24)
                  .width(24)
                  .onChange((isChecked: boolean) => {
                    this.isSilentMode = "INTERACTIVE_MODE"
                  })
                Text("动作活体检测")
                  .fontSize(16)
              }
              .margin({ right: 15 })

              Row() {
                Radio({ value: "SILENT_MODE", group: "isSilentMode" }).checked(false)
                  .height(24)
                  .width(24)
                  .onChange((isChecked: boolean) => {
                    this.isSilentMode = "SILENT_MODE";
                  })
                Text("静默活体检测")
                  .fontSize(16)
              }
            }
            .width("75%")
          }
        }
        .margin({ bottom: 30 })

        Row() {
          Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
            Text("验证完的跳转模式:")
              .fontSize(18)
              .width("25%")
            Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
              Row() {
                Radio({ value: "replace", group: "routeMode" }).checked(true)
                  .height(24)
                  .width(24)
                  .onChange((isChecked: boolean) => {
                    this.routeMode = "replace"
                  })
                Text("replace")
                  .fontSize(16)
              }
              .margin({ right: 15 })

              Row() {
                Radio({ value: "back", group: "routeMode" }).checked(false)
                  .height(24)
                  .width(24)
                  .onChange((isChecked: boolean) => {
                    this.routeMode = "back";
                  })
                Text("back")
                  .fontSize(16)
              }
            }
            .width("75%")
          }
        }
        .margin({ bottom: 30 })

        if (this.isSilentMode == "INTERACTIVE_MODE") {
          Row() {
            Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
              Text("动作数量:")
                .fontSize(18)
                .width("25%")
              TextInput({
                placeholder: this.actionsNum != 0 ? this.actionsNum.toString() : "动作数量最多4个"
              })
                .type(InputType.Number)
                .placeholderFont({
                  size: 18,
                  weight: FontWeight.Normal,
                  family: "HarmonyHeiTi",
                  style: FontStyle.Normal
                })
                .fontSize(18)
                .fontWeight(FontWeight.Bold)
                .fontFamily("HarmonyHeiTi")
                .fontStyle(FontStyle.Normal)
                .width("65%")
                .onChange((value: string) => {
                  this.actionsNum = Number(value) as interactiveLiveness.ActionsNumber;
                })
            }
          }
        }
      }
      .margin({ left: 24, top: 80 })
      .zIndex(1)

      Stack({
        alignContent: Alignment.Bottom
      }) {
        if (this.resultInfo?.mPixelMap) {
          Image(this.resultInfo?.mPixelMap)
            .width(260)
            .height(260)
            .align(Alignment.Center)
            .margin({ bottom: 260 })
          Circle()
            .width(300)
            .height(300)
            .fillOpacity(0)
            .strokeWidth(60)
            .stroke(Color.White)
            .margin({ bottom: 250, left: 0 })
        }

        Text(this.resultInfo.mPixelMap ?
          "检测成功" :
          this.failResult.code != 1008302000 ?
            "检测失败" :
            "")
          .width("100%")
          .height(26)
          .fontSize(20)
          .fontColor("#000000")
          .fontFamily("HarmonyHeiTi")
          .margin({ top: 50 })
          .textAlign(TextAlign.Center)
          .fontWeight("Medium")
          .margin({ bottom: 240 })

        if(this.failResult.code != 1008302000) {
          Text(this.failResult.message as string)
            .width("100%")
            .height(26)
            .fontSize(16)
            .fontColor(Color.Gray)
            .textAlign(TextAlign.Center)
            .fontFamily("HarmonyHeiTi")
            .fontWeight("Medium")
            .margin({ bottom: 200 })
        }

        Button("开始检测", { type: ButtonType.Normal, stateEffect: true })
          .width(192)
          .height(40)
          .fontSize(16)
          .backgroundColor(0x317aff)
          .borderRadius(20)
          .margin({
            bottom: 56
          })
          .onClick(() => {
            this.privatestartDetection();
          })
      }
      .height("100%")
    }
  }

  onPageShow() {
    this.resultRelease();
    this.getDectionRsultInfo();
  }

  // 路由跳转到人脸活体验证控件
  private privaterouterLibrary() {
    let routerOptions: interactiveLiveness.InteractiveLivenessConfig = {
      "isSilentMode": this.isSilentMode as interactiveLiveness.DetectionMode,
      "routeMode": this.routeMode as interactiveLiveness.RouteRedirectionMode,
      "actionsNum": this.actionsNum
    }

    interactiveLiveness.startLivenessDetection(routerOptions).then((DetectState: boolean) => {
      hilog.info(0x0001, "LivenessCollectionIndex", `Succeeded in jumping.`);
    }).catch((err: BusinessError) => {
      hilog.error(0x0001, "LivenessCollectionIndex", `Failed to jump. Code:${err.code},message:${err.message}`);
    })
  }

  // 校验CAMERA权限
  private privatestartDetection() {
    abilityAccessCtrl.createAtManager().requestPermissionsFromUser(this.context, this.array).then((res) => {
      for (let i = 0; i < res.permissions.length; i++) {
        if (res.permissions[i] === "ohos.permission.CAMERA" && res.authResults[i] === 0) {
          this.privaterouterLibrary();
        }
      }
    })
  }

  // 获取验证结果
  private getDectionRsultInfo() {
    // getInteractiveLivenessResult接口调用完会释放资源
    let resultInfo = interactiveLiveness.getInteractiveLivenessResult();
    resultInfo.then(data => {
      this.resultInfo = data;
    }).catch((err: BusinessError) => {
      this.failResult = {
        "code": err.code,
        "message": err.message
      }
    })
  }

  // result release
  private resultRelease() {
    this.resultInfo = {
      livenessType: 0
    }
    this.failResult = {
      "code": 1008302000,
      "message": ""
    }
  }
}
分享
微博
QQ
微信
回复
22h前
相关问题
HarmonyOS 人脸活体检测调用
72浏览 • 1回复 待解决
点击FindComponentById之后没有任何反应
7715浏览 • 2回复 待解决
startScroll()没有任何反应
6742浏览 • 2回复 待解决
HarmonyOS 人脸活体检测问题
40浏览 • 1回复 待解决
HarmonyOS 人脸活体检测Vision Kit
69浏览 • 1回复 待解决
HarmonyOS 版本更新及人脸识别
41浏览 • 1回复 待解决
HarmonyOS 人脸识别回调问题
46浏览 • 1回复 待解决
使用华为支付,调用收银台没有反应
560浏览 • 1回复 待解决
在@watch中使用异步方法UI反应
448浏览 • 1回复 待解决
HarmonyOS 活体检测和卡证识别的demo
1浏览 • 0回复 待解决
如何使用原生能力人脸识别api?
258浏览 • 1回复 待解决