// // CheckBodyManager.m // jiaPei // // Created by 张嵘 on 2021/1/7. // Copyright © 2021 JCZ. All rights reserved. // #import "CheckBodyManager.h" /// 百度人脸识别 #import "BDFaceLivenessViewController.h" #import "BDFaceDetectionViewController.h" #import "BDFaceLivingConfigViewController.h" #import "BDFaceAgreementViewController.h" #import "BDFaceLogoView.h" #import "BDFaceLog.h" #import "BDFaceCalculateTool.h" #import "BDFaceToastView.h" #import "BDFaceImageShow.h" #import "BDFaceLivingConfigModel.h" static CheckBodyManager *manger = nil; static dispatch_once_t onceToken; @interface CheckBodyManager () @property (nonatomic, readwrite, copy) NSArray *waterMarkArr; @property (nonatomic, readwrite, strong) CheckBodyCompletedBlock checkBodyCompletedBlock;//定位回调 @property (nonatomic, readwrite, copy) NSArray *allLiveActionArr; @property (nonatomic, readwrite, assign) RQCheckBodyType checkBodyType; @property (nonatomic, readwrite, strong) UIImage *waterImage; @property (nonatomic, readwrite, assign) NSUInteger index; @end @implementation CheckBodyManager + (CheckBodyManager *)sharedManager { dispatch_once(&onceToken, ^{ manger = [[self alloc] init]; }); return manger; } - (instancetype)init { self = [super init]; if (self) { } return self; } #pragma mark - BDFaceSDK - (void)beginCheckBodyWithCheckNum:(NSInteger)num completeBlock:(CheckBodyCompletedBlock)completeBlock { [self beginCheckBodyWithCheckNum:num checkBodyType:RQCheckBodyType_Default waterMarkArr:nil completeBlock:completeBlock]; } - (void)beginCheckBodyWithCheckNum:(NSInteger)num checkBodyType:(RQCheckBodyType)checkBodyType waterMarkArr:(NSArray * _Nullable )waterMarkArr completeBlock:(CheckBodyCompletedBlock)completeBlock { self.checkBodyCompletedBlock = completeBlock; self.checkBodyType = checkBodyType; self.waterMarkArr = waterMarkArr; //2023-7-11 ning 新需求 id alertMsg = [RQ_USER_MANAGER getParamsKey:[NSString stringWithFormat:@"SP_ALERT_%@",RQ_USER_MANAGER.currentUser.city]]; if(alertMsg){ ShowMsg(alertMsg); } // 读取设置配置,启动活体检测与否 NSNumber *LiveMode = [[NSUserDefaults standardUserDefaults] objectForKey:@"LiveMode"]; if (LiveMode.boolValue){ [self faceLivenessWithCheckNum:num]; } else { [self faceDetect]; } } - (void)faceDetect { @weakify(self) BDFaceDetectionViewController* dvc = [[BDFaceDetectionViewController alloc] init]; [dvc initWithCompleteBlock:^(BOOL success) { @strongify(self) if (success) { [self getImageStrWithImage:[[BDFaceImageShow sharedInstance] getSuccessImage] completeBlock:^(NSString *backString) { if (self.checkBodyCompletedBlock && RQStringIsNotEmpty(backString)) { self.checkBodyCompletedBlock(YES, @{@"normalImg" : backString, @"actionImg" : [RQ_CHECKBODY_MANAGER getActionImageString]}); } }]; } else { if (self.checkBodyCompletedBlock) { self.checkBodyCompletedBlock(NO, nil); } } [[IDLFaceLivenessManager sharedInstance] reset]; [[BDFaceImageShow sharedInstance] reset]; }]; UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:dvc]; navi.navigationBarHidden = true; navi.modalPresentationStyle = UIModalPresentationFullScreen; [QMUIHelper.visibleViewController presentViewController:navi animated:YES completion:nil]; } - (void)faceLivenessWithCheckNum:(NSInteger)num { @weakify(self) if (num == 0) { [RQ_SHARE_FUNCTION getPhotosWithGetPhotosWay:GetPhotosWay_Camera size:kSize maxLength:200 maxImagesCount:1 isCheckBody:YES photosBlock:^(NSArray * _Nonnull imagesArr) { if (imagesArr.count > 0) { [self getImageStrWithImage:imagesArr.firstObject completeBlock:^(NSString *backString) { if (self.checkBodyCompletedBlock && RQStringIsNotEmpty(backString)) { self.checkBodyCompletedBlock(YES, @{@"normalImg" : backString, @"actionImg" : backString}); } }]; } else { if (self.checkBodyCompletedBlock) { self.checkBodyCompletedBlock(NO, nil); } } }]; } else { BDFaceLivenessViewController* lvc = [[BDFaceLivenessViewController alloc] init]; [[IDLFaceLivenessManager sharedInstance] reset]; [[BDFaceImageShow sharedInstance] reset]; NSMutableArray *liveActionBeginArray = self.allLiveActionArr.mutableCopy; NSMutableArray *liveActionEndArray = @[].mutableCopy; if (num != 1) { [liveActionBeginArray removeObject:@(FaceLivenessActionTypeLiveMouth)]; [liveActionEndArray addObject:@(FaceLivenessActionTypeLiveMouth)]; NSInteger a = num - 1; num = (a >= 0)? a : 0; } NSInteger b = liveActionBeginArray.count; for (int i = 0; i < ((num > b)? b : num); i++) { int r = arc4random() % [liveActionBeginArray count]; [liveActionEndArray addObject:liveActionBeginArray[r]]; [liveActionBeginArray removeObject:liveActionBeginArray[r]]; } BDFaceLivingConfigModel *faceLivingConfigModel = [BDFaceLivingConfigModel sharedInstance]; if (liveActionEndArray.count > 0) { faceLivingConfigModel.liveActionArray = liveActionEndArray; faceLivingConfigModel.numOfLiveness = liveActionEndArray.count; faceLivingConfigModel.isByOrder = YES; } [lvc livenesswithList:faceLivingConfigModel.liveActionArray order:faceLivingConfigModel.isByOrder numberOfLiveness:faceLivingConfigModel.numOfLiveness]; [lvc initWithCompleteBlock:^(BOOL success) { @strongify(self) if (success) { [self getImageStrWithImage:[[BDFaceImageShow sharedInstance] getSuccessImage] completeBlock:^(NSString *backString) { if (self.checkBodyCompletedBlock && RQStringIsNotEmpty(backString)) { self.checkBodyCompletedBlock(YES, @{@"normalImg" : backString, @"actionImg" : [RQ_CHECKBODY_MANAGER getActionImageString]}); } }]; } else { if (self.checkBodyCompletedBlock) { self.checkBodyCompletedBlock(NO, nil); } } }]; UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:lvc]; navi.modalPresentationStyle = UIModalPresentationFullScreen; [QMUIHelper.visibleViewController presentViewController:navi animated:YES completion:nil]; } } /// 设置活体动作图片base64字符串 /// @param actionImageString 活体动作图片base64字符串 - (void)setActionImageString:(NSString *)actionImageString { _actionImageString = actionImageString; } /// 获取活体动作图片base64字符串 - (NSString *)getActionImageString{ return self.actionImageString? : @""; } - (void)getImageStrWithImage:(UIImage *)image completeBlock:(void(^)(NSString *backString))completeBlock { @weakify(self) UIImage *normalImage = [UIImage fixOrientation:image]; CGSize imageSize = CGSizeMake(260, 378); UIImage *newImage; switch (self.checkBodyType) { case RQCheckBodyType_Default: imageSize = CGSizeMake(260, 378); newImage = [normalImage qmui_imageResizedInLimitedSize:imageSize]; break; case RQCheckBodyType_PTSC: { CGFloat width = [[RQ_COMMON_MANAGER.JSJP_APP_PTSC_FBL componentsSeparatedByString:@","].firstObject floatValue]; CGFloat height = [[RQ_COMMON_MANAGER.JSJP_APP_PTSC_FBL componentsSeparatedByString:@","].lastObject floatValue]; imageSize = CGSizeMake(width, height); newImage = [normalImage qmui_imageWithClippedRect:CGRectMake(0, normalImage.size.width / 4.f, normalImage.size.width, normalImage.size.width)]; newImage = [newImage qmui_imageResizedInLimitedSize:imageSize resizingMode:QMUIImageResizingModeScaleAspectFill]; // imageSize = CGSizeMake(320, 240); // newImage = [normalImage qmui_imageWithClippedRect:CGRectMake(0, normalImage.size.width / 4.f, normalImage.size.width, normalImage.size.width)]; // CGFloat a = (normalImage.size.width * (32 / 24) - normalImage.size.width) / 2.f; // newImage = [newImage qmui_imageWithSpacingExtensionInsets:UIEdgeInsetsMake(0, a, 0, - a)]; // newImage = [newImage qmui_imageResizedInLimitedSize:imageSize resizingMode:QMUIImageResizingModeScaleAspectFill]; break; } default: break; } if (!RQObjectIsNil(self.waterMarkArr) && self.waterMarkArr.count > 1) { self.waterImage = newImage; __block NSUInteger myIndex = 0; [[self.waterMarkArr.rac_sequence.signal deliverOnMainThread] subscribeNext:^(NSString *waterMarkStr) { @strongify(self) self.index = myIndex; CGPoint point = CGPointZero; CGFloat strHeight = [waterMarkStr heightForFont:RQRegularFont(20) width:RQ_SCREEN_WIDTH - 32]; if (self.index < 4) { CGFloat spaceHeight = (self.index > 0)? 8 : 0; point = CGPointMake(16, 16 + self.index * strHeight + (self.index * spaceHeight)); } else { CGFloat spaceHeight = (self.index == 4)? 0 : 8; point = CGPointMake(16, newImage.size.height - 16 - (strHeight * (self.index - 3)) - (spaceHeight * (self.index - 4))); } self.waterImage = [self.waterImage imageWaterMarkWithString:waterMarkStr point:point attribute:@{NSForegroundColorAttributeName:UIColor.whiteColor,NSFontAttributeName:RQRegularFont(20)}]; myIndex ++; } completed:^{ NSData *imgData = [UIImage resetSizeOfImageData:self.waterImage maxSize:200]; NSString *backString = [NSString stringWithFormat:@"%@",[imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]]; if (completeBlock) { completeBlock(backString); } }]; } else { NSData *imgData = [UIImage resetSizeOfImageData:newImage maxSize:200]; NSString *backString = [NSString stringWithFormat:@"%@",[imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]]; if (completeBlock) { completeBlock(backString); } } } - (NSArray *)allLiveActionArr { return @[@(FaceLivenessActionTypeLiveEye), @(FaceLivenessActionTypeLiveMouth), @(FaceLivenessActionTypeLiveYawRight), @(FaceLivenessActionTypeLiveYawLeft), @(FaceLivenessActionTypeLivePitchUp), @(FaceLivenessActionTypeLivePitchDown), ]; } @end