HarmonyOS 人脸活体检测问题

在使用人脸活体检测成功后,如何在调起页面获取检测完的结果以及获取人脸图片。

根据https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/vision-interactiveliveness-V5?catalogVersion=V5的说明,使用的routeMode为back方式,将获取结果的方法写在onPageShow或者aboutToAppear中,都无法获取检测结果。

HarmonyOS
2024-12-25 18:00:59
879浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

请参考示例如下:

import { abilityAccessCtrl, common, Permissions } from '@kit.AbilityKit';
import { interactiveLiveness } from '@kit.VisionKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { faceComparator } from '@kit.CoreVisionKit';
import { TAG } from '@ohos/hypium/src/main/Constant';

export class FaceCheck {

  // 校验CAMERA权限
  public privateStartDetection(context: common.UIAbilityContext, permissions: Permissions[]): Promise<boolean> {
    return new Promise((resolve, reject) => {
      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) {
            resolve(true)
          } else {
            reject(false)
          }
        }
      })
    })
  }

  // 路由跳转到人脸活体验证控件
  public privateRouterLibrary(isSilentMode: string = 'SILENT_MODE', routeMode: string = "replace",
    actionsNum = 1): Promise<boolean> {
    let routerOptions: interactiveLiveness.InteractiveLivenessConfig = {
      isSilentMode: isSilentMode as interactiveLiveness.DetectionMode,
      routeMode: routeMode as interactiveLiveness.RouteRedirectionMode,
      actionsNum: actionsNum
    }

    return new Promise((resolve, reject) => {
      interactiveLiveness.startLivenessDetection(routerOptions).then((DetectState: boolean) => {
        return resolve(DetectState)


      }).catch((err: BusinessError) => {
        hilog.error(0x0001, "LivenessCollectionIndex", `Failed to jump. Code:${err.code},message:${err.message}`);
        reject(false)
      })
    })
  }

  public async faceCompare(pixelMap: PixelMap,chooseImage1:PixelMap,dataValues:string): Promise<string> {
    if (!pixelMap || !chooseImage1) {
      hilog.error(0x0000, TAG, "Failed to choose image");
      return '';
    }
    // 调用人脸比对接口
    let visionInfo: faceComparator.VisionInfo = {
      pixelMap: pixelMap,
    };
    let visionInfo1: faceComparator.VisionInfo = {
      pixelMap: chooseImage1,
    };
    let data: faceComparator.FaceCompareResult = await faceComparator.compareFaces(visionInfo, visionInfo1);
    let faceString =
      "degree of similarity:" + this.toPercentage(data.similarity) + ((data.isSamePerson) ? ". is" : ". no") +
        "same person";
    hilog.info(0x0000, TAG, "faceString data is " + faceString);
    dataValues = faceString;
    return dataValues
  }

  private toPercentage(num: number): string {
    return `${(num * 100).toFixed(2)}%`;
  }
}
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 FacePage {
  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": ""
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
  • 270.
  • 271.
  • 272.
  • 273.
  • 274.
  • 275.
  • 276.
  • 277.
  • 278.
  • 279.
  • 280.
  • 281.
  • 282.
  • 283.
  • 284.
  • 285.
  • 286.
  • 287.
  • 288.
  • 289.
  • 290.
  • 291.
  • 292.
  • 293.
  • 294.
  • 295.
  • 296.
  • 297.
  • 298.
  • 299.
  • 300.
  • 301.
  • 302.
  • 303.
  • 304.
  • 305.
  • 306.
  • 307.
  • 308.
  • 309.
  • 310.
  • 311.
  • 312.
  • 313.
分享
微博
QQ
微信
回复
2024-12-25 19:21:27
相关问题
HarmonyOS 人脸活体检测调用
913浏览 • 1回复 待解决
HarmonyOS 人脸活体检测Vision Kit
985浏览 • 1回复 待解决
HarmonyOS 是否有人脸活体检测API支持
672浏览 • 1回复 待解决
HarmonyOS 活体检测
806浏览 • 1回复 待解决
HarmonyOS 活体检测图片返回问题
687浏览 • 1回复 待解决
HarmonyOS 活体检测回调问题
1013浏览 • 1回复 待解决
HarmonyOS 活体检测失败
842浏览 • 1回复 待解决
HarmonyOS自带的活体检测
874浏览 • 1回复 待解决
HarmonyOS 华为活体检测测试报告
1082浏览 • 1回复 待解决
HarmonyOS 有没有活体检测的SDK?
895浏览 • 1回复 待解决
HarmonyOS 活体检测和卡证识别的demo
835浏览 • 1回复 待解决
HarmonyOS人脸活体认证
922浏览 • 1回复 待解决
HarmonyOS 人脸检测
806浏览 • 1回复 待解决