快速集成指南

注意事项

license文件(DFLicense)用以控制程序的包名和有效时间(时间范围可以直接查看文件内容获取),请用户一定要确认程序的包名是否与 license 绑定的包名一致,请确保程序运行设备的系统时间在 license 的有效时间内。

SDK 目录结构

目录

将SDK集成到开发环境

使用SDK前,首先需要将其集成到您的开发环境中。

配置开发环境

1. 导入SDK包

将libDFActionLiveness文件夹(包含 libDFActionLivenessDetector.a、DFLicense、df_liveness_resource.bundle、.h、.m等) copy 一份到项目工程目录下,拖拽到 xcode 打开的工程中,勾选 copy,点击 Finish 按钮。

注:本 SDK 不支持 CocoaPods 的方式导入。

示例介绍图

2. 编译选项设置
2.1 需要添加 Xcode 链接器参数:-ObjC 和 -lstdc++
  • 添加 -ObjC 参数后链接器可以把静态库中所有的 Objective-C 类和分类都加载到最后的可执行文件中。
  • 添加 -lstdc++ 参数是由于我们的静态库中需要 c++ 标准库支持。
  • 添加方法: TARGETS -> Build Settings -> Linking -> Other Linker Flags 中添加 -lstdc++ 和 -ObjC 。

示例介绍图

2.2 需要手动关闭Bitcode。

步骤:

TARGETS -> BuildSettings -> Enable Bitcode 设置为 NO。

示例介绍图

3. 添加相关引用库。
  • 步骤: TARGETS ->Build Phases中添加相关引用库 示例介绍图

4. 调试iOS9以上系统时,调用相机功能时,在info.plist文件下添加隐私权限。
 <key>NSCameraUsageDescription</key>
 <string>cameraDesciption</string>
 <key> NSPhotoLibraryUsageDescription </key>
 <string>cameraDesciption</string>

开始检测

DFActionfaceViewController调用流程

DFActionfaceViewController 是我们提供的活体检测SDK示例。可以设置动作序列等操作。设置完成后,就可以开始进行检测。

详细说明如下:

  1. 遵守 DFActionLivenessDetectorDelegate 协议

     @interface ViewController () <DFActionLivenessDetectorDelegate>
    
  2. 获取资源路径

     NSString *strResourcesBundlePath = [[NSBundle mainBundle] pathForResource:@"df_liveness_resource" ofType:@"bundle"];
    
  3. 获取授权文件路径

     NSString *licensePath = [[NSBundle mainBundle] pathForResource:@"DFLicense" ofType:@""];
    
  4. 初始化活体检测视图控制器

     DFActionfaceViewController *livenessVC = [[DFActionfaceViewController alloc] initWithDuration:10.0f resourcesBundlePath:strResourcesBundlePath licensePath:licensePath];
    
  5. 相关参数设置

    你需要自己设置动作序列、各动作阈值。SDK会根据您的设置,来进行活体检测。必须把静默 HOLDSTILL 放第一个且只能有一个静默动作,后面可以随意组合。

     //可以根据实际需求自由组合,第一个动作必须要为静默且序列中只能有一个静默动作
     NSArray *arrLivenessSequence = @[@(LIVE_HOLD_STILL), @(LIVE_BLINK) , @(LIVE_MOUTH) , @(LIVE_NOD) , @(LIVE_YAW)];
     NSArray *arrThreshold = @[@(0.7) , @(0.7) , @(0.7) , @(0.7) , @(0.7)];
    
     // 设置代理、回调线程、动作序列和动作对应阈值
     [livefaceVC setDelegate:self callBackQueue:dispatch_get_main_queue() detectionSequence:arrLivenessSequence detectionThreshold:arrThreshold];
    
     // 设置活体检测输出模式
     [livenessVC setOutputType:LIVE_OUTPUT_MULTI_IMAGE];
    
  6. Present 活体检测视图控制器

     UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:livenessVC];
     [navigationController setNavigationBarHidden:YES];
     [self presentViewController:navigationController animated:YES completion:nil];
    
  7. 实现 DFActionLivenessDetectorDelegate 中的方法

     - (void)livenessDidStartDetectionWithDetectionType:(LivefaceDetectionType)iDetectionType
                                         detectionIndex:(int)iDetectionIndex;
     {
         // 每个动作开始检测时会回调此方法
     }
    
     - (void)livenessTimeDidPast:(double)dPast durationPerModel:(double)dDurationPerModel
     {
         // 检测开始后,当初始化检测器时如果Duration大于0则每帧都会回调此方法(示例程序中为10),dPast为当前动作已耗时,dDurationPerModel为每个动作设定的最大时间
     }
    
     // 活体检测成功的回调 , data为加密后的数据 , arrDFImage 为 DFImage 对象的数组, dfVideoData 为检测时的视频数据(目前为空)
     - (void)livenessDidSuccessfulGetData:(NSData *)data
                                 dfImages:(NSArray *)arrDFImage
                              dfVideoData:(NSData *)dfVideoData
     {   
     }
    
     // 活体检测失败的回调 , iErrorType为失败的错误类型 , iDetectionType为失败时检测类型 , iDetectionIndex为失败时检测类型所在检测序列中的位置(0为第一个)
     - (void)livenessDidFailWithErrorType:(LivefaceErrorType)iErrorType
                            detectionType:(LivefaceDetectionType)iDetectionType
                           detectionIndex:(int)iDetectionIndex
                                     data:(NSData *)data
                                 dfImages:(NSArray *)arrDFImage
                              dfVideoData:(NSData *)dfVideoData
     {
     }
    
     // 活体检测取消的回调 , iDetectionType为活体检测取消时的检测类型 , iDetectionIndex为取消时检测类型所在检测序列中的位置(0 为第一个)
     - (void)livenessDidCancelWithDetectionType:(LivefaceDetectionType)iDetectionType detectionIndex:(int)iDetectionIndex
     {
     }
    

DFActionLivenessController 调用流程

DFActionLivenessController是对DFActionfaceViewController的一层封装,已经写好了一部分内部逻辑。如果没有内部定制化的要求,可以直接使用DFActionLivenessController。

  1. 遵守 DFActionLivenessDetectorDelegate 协议

     @interface ViewController () <DFActionLivenessDelegate>
    
  2. 初始化参数设置

    DFActionLivenessController是以json串作为初始化参数的

     //设置输出模式 multiImg 唯一模式 其他暂不支持
     NSString *outType = @"multiImg";
    
     //设置动作序列 
     //可以根据实际需求自由组合,第一个动作必须要为静默且序列中只能有一个静默动作
     NSArray *sequence = @[@"HOLDSTILL", @"BLINK", @"MOUTH", @"NOD", @"YAW"];
    
     // 设置检测阈值
     NSArray *threshold = @[@(0.7), @(0.7), @(0.7), @(0.7), @(0.7)];
    
     // autoAntiHack:自动上传活体加密文件做antiHack
     NSDictionary *dictJson = @{@"sequence":sequence,
                                 @"outType":outType,
                               @"threshold":threshold, 
                            @"autoAntiHack":@(YES)};
     //转化为json字符串
     NSString *strJson = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dictJson options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
     DFActionLivenessController *actionLiveVC = [[DFActionLivenessController alloc] init];
     [actionLiveVC setJsonCommand:strJson];
    
  3. Present 活体检测视图控制器

     UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:actionLiveVC];
     [navigationController setNavigationBarHidden:YES];
     [self presentViewController:navigationController animated:YES completion:^{
             //如果需要自动开始,而不是按钮触发,调用下面这行代码
             //[actionLiveVC restart];
     }];
    
  4. 实现 DFActionLivenessDelegate 中的方法

     // 活体检测已经开始的回调     
     - (void)actionLivenessDidStart;
    
     // 活体检测成功回调方法 当设置 autoAntiHack 为 NO 时回调
     - (void)actionLivenessDidSuccessfulGetData:(NSData *)encryTarData dfImages:(NSArray *)arrDFImage dfVideoData:(NSData *)dfVideoData
     {
         // 调用防hack接口,上传encryTarData,获取antiHack结果
         // 上传参考DEMO
     }
    
     // 活体检测成功回调方法 当设置 autoAntiHack 为 YES 时回调
     - (void)actionLivenessDidSuccessfulGetData:(NSData *)encryTarData dfImages:(NSArray *)arrDFImage dfVideoData:(NSData *)dfVideoData isHack:(BOOL)isHack
     {
         // isHack 是上传encryTarData后的antiHack结果
     }
    
     // 活体检测失败回调方法.(可根据需要添加参数返回失败时的图片,视频为空)   
     - (void)actionLivenessDidFailWithType:(DFMultipleLivenessError)iErrorType DetectionType:(DFDetectionType)iDetectionType DetectionIndex:(NSInteger)iIndex Data:(NSData *)encryTarData dfImages:(NSArray *)arrDFImage dfVideoData:(NSData *)dfVideoData;
    
     // 取消活体检测指令回调方法
     - (void)actionLivenessDidCancel;
    

results matching ""

    No results matching ""