先说重点!!!
一定要先先校验相机权限,否则是不会成功调去活体检测功能
一.校验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()