鸿蒙活体检测功能interactiveLiveness

先说重点!!!

一定要先先校验相机权限,否则是不会成功调去活体检测功能

一.校验CAMERA权限

 // 校验CAMERA权限
  public privateStartDetection(successfulRouteUrl?:string,type?:number) {
    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(successfulRouteUrl,type);
        }
      }
    })
  }

二.路由跳转到人脸活体验证控件

// 路由跳转到人脸活体验证控件
  public privateRouterLibrary(successfulRouteUrl?:string,type?:number) {
    let isSilentMode = "INTERACTIVE_MODE" as interactiveLiveness.DetectionMode;
    let routeMode = type ==1 ? 'back'as interactiveLiveness.RouteRedirectionMode :"replace" as interactiveLiveness.RouteRedirectionMode;
    let actionsNum =1  as interactiveLiveness.ActionsNumber;
    let routerOptions: interactiveLiveness.InteractiveLivenessConfig = {
      "isSilentMode": isSilentMode,
      "routeMode": routeMode,
      "actionsNum": actionsNum,
      "successfulRouteUrl":successfulRouteUrl,
    }

  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}`);
    })
  }

三.获取活体检测返回的图片

这里要说下:····他这个返回的图片是mPixelMap格式,所以需要转成你需要的格式,以下的示例代码为base64格式

// 获取活体检测返回的图片
  public getDectionResultInfo() {
  this.base64 = ''
    // getInteractiveLivenessResult接口调用完会释放资源
    let resultInfo = interactiveLiveness.getInteractiveLivenessResult();
    resultInfo.then(data => {
      this.pixelMapBase(data.mPixelMap)
      console.log(JSON.stringify(data.mPixelMap))
    }).catch((err: BusinessError) => {
      // promptAction.showToast({ message: err.message })
    })
  }

四.格式转换方法   mPixelMap转换为base64

 public pixelMapBase(value?:image.PixelMap) {
    const imagePackerApi: image.ImagePacker =  image.createImagePacker();
    let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 100 };
    imagePackerApi.packing(value,packOpts).then( (data: ArrayBuffer) => {
      let buf: buffer.Buffer =   buffer.from(data);
      this.base64 =  buf.toString('base64', 0, buf.length);
      console.log('base64: ' + this.base64);
    })
  }

五.完整代码



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

export class bioassayUtils {
  //活体
  private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
  private array: Array<Permissions> = ["ohos.permission.CAMERA"];
  public base64 :string = ''
  /*
    先校验权限然后跳转   必须先校验!!!/
   */
  // 校验CAMERA权限
  public privateStartDetection(successfulRouteUrl?:string,type?:number) {
    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(successfulRouteUrl,type);
        }
      }
    })
  }
  // 路由跳转到人脸活体验证控件
  public privateRouterLibrary(successfulRouteUrl?:string,type?:number) {
    let isSilentMode = "INTERACTIVE_MODE" as interactiveLiveness.DetectionMode;
    let routeMode = type ==1 ? 'back'as interactiveLiveness.RouteRedirectionMode :"replace" as interactiveLiveness.RouteRedirectionMode;
    let actionsNum =1  as interactiveLiveness.ActionsNumber;
    let routerOptions: interactiveLiveness.InteractiveLivenessConfig = {
      "isSilentMode": isSilentMode,
      "routeMode": routeMode,
      "actionsNum": actionsNum,
      "successfulRouteUrl":successfulRouteUrl,
    }

  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}`);
    })
  }
  // 获取活体检测返回的图片
  public getDectionResultInfo() {
  this.base64 = ''
    // getInteractiveLivenessResult接口调用完会释放资源
    let resultInfo = interactiveLiveness.getInteractiveLivenessResult();
    resultInfo.then(data => {
      this.pixelMapBase(data.mPixelMap)
      console.log(JSON.stringify(data.mPixelMap))
    }).catch((err: BusinessError) => {
      // promptAction.showToast({ message: err.message })
    })
  }
  public pixelMapBase(value?:image.PixelMap) {
    const imagePackerApi: image.ImagePacker =  image.createImagePacker();
    let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 100 };
    imagePackerApi.packing(value,packOpts).then( (data: ArrayBuffer) => {
      let buf: buffer.Buffer =   buffer.from(data);
      this.base64 =  buf.toString('base64', 0, buf.length);
      console.log('base64: ' + this.base64);
    })
  }
}

export  default  new bioassayUtils()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值