CheckBodyManager.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // CheckBodyManager.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2021/1/7.
  6. // Copyright © 2021 JCZ. All rights reserved.
  7. //
  8. #import "CheckBodyManager.h"
  9. /// 百度人脸识别
  10. #import "BDFaceLivenessViewController.h"
  11. #import "BDFaceDetectionViewController.h"
  12. #import "BDFaceLivingConfigViewController.h"
  13. #import "BDFaceAgreementViewController.h"
  14. #import "BDFaceLogoView.h"
  15. #import "BDFaceLog.h"
  16. #import "BDFaceCalculateTool.h"
  17. #import "BDFaceToastView.h"
  18. #import "BDFaceImageShow.h"
  19. #import "BDFaceLivingConfigModel.h"
  20. static CheckBodyManager *manger = nil;
  21. static dispatch_once_t onceToken;
  22. @interface CheckBodyManager ()
  23. @property (nonatomic, readwrite, copy) NSArray *waterMarkArr;
  24. @property (nonatomic, readwrite, strong) CheckBodyCompletedBlock checkBodyCompletedBlock;//定位回调
  25. @property (nonatomic, readwrite, copy) NSArray *allLiveActionArr;
  26. @property (nonatomic, readwrite, assign) RQCheckBodyType checkBodyType;
  27. @property (nonatomic, readwrite, strong) UIImage *waterImage;
  28. @property (nonatomic, readwrite, assign) NSUInteger index;
  29. @end
  30. @implementation CheckBodyManager
  31. + (CheckBodyManager *)sharedManager {
  32. dispatch_once(&onceToken, ^{
  33. manger = [[self alloc] init];
  34. });
  35. return manger;
  36. }
  37. - (instancetype)init {
  38. self = [super init];
  39. if (self) {
  40. }
  41. return self;
  42. }
  43. #pragma mark - BDFaceSDK
  44. - (void)beginCheckBodyWithCheckNum:(NSInteger)num completeBlock:(CheckBodyCompletedBlock)completeBlock {
  45. [self beginCheckBodyWithCheckNum:num checkBodyType:RQCheckBodyType_Default waterMarkArr:nil completeBlock:completeBlock];
  46. }
  47. - (void)beginCheckBodyWithCheckNum:(NSInteger)num checkBodyType:(RQCheckBodyType)checkBodyType waterMarkArr:(NSArray * _Nullable )waterMarkArr completeBlock:(CheckBodyCompletedBlock)completeBlock {
  48. self.checkBodyCompletedBlock = completeBlock;
  49. self.checkBodyType = checkBodyType;
  50. self.waterMarkArr = waterMarkArr;
  51. //2023-7-11 ning 新需求
  52. id alertMsg = [RQ_USER_MANAGER getParamsKey:[NSString stringWithFormat:@"SP_ALERT_%@",RQ_USER_MANAGER.currentUser.city]];
  53. if(alertMsg){
  54. ShowMsg(alertMsg);
  55. }
  56. // 读取设置配置,启动活体检测与否
  57. NSNumber *LiveMode = [[NSUserDefaults standardUserDefaults] objectForKey:@"LiveMode"];
  58. if (LiveMode.boolValue){
  59. [self faceLivenessWithCheckNum:num];
  60. } else {
  61. [self faceDetect];
  62. }
  63. }
  64. - (void)faceDetect {
  65. @weakify(self)
  66. BDFaceDetectionViewController* dvc = [[BDFaceDetectionViewController alloc] init];
  67. [dvc initWithCompleteBlock:^(BOOL success) {
  68. @strongify(self)
  69. if (success) {
  70. [self getImageStrWithImage:[[BDFaceImageShow sharedInstance] getSuccessImage] completeBlock:^(NSString *backString) {
  71. if (self.checkBodyCompletedBlock && RQStringIsNotEmpty(backString)) {
  72. self.checkBodyCompletedBlock(YES, @{@"normalImg" : backString, @"actionImg" : [RQ_CHECKBODY_MANAGER getActionImageString]});
  73. }
  74. }];
  75. } else {
  76. if (self.checkBodyCompletedBlock) {
  77. self.checkBodyCompletedBlock(NO, nil);
  78. }
  79. }
  80. [[IDLFaceLivenessManager sharedInstance] reset];
  81. [[BDFaceImageShow sharedInstance] reset];
  82. }];
  83. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:dvc];
  84. navi.navigationBarHidden = true;
  85. navi.modalPresentationStyle = UIModalPresentationFullScreen;
  86. [QMUIHelper.visibleViewController presentViewController:navi animated:YES completion:nil];
  87. }
  88. - (void)faceLivenessWithCheckNum:(NSInteger)num {
  89. @weakify(self)
  90. if (num == 0) {
  91. [RQ_SHARE_FUNCTION getPhotosWithGetPhotosWay:GetPhotosWay_Camera size:kSize maxLength:200 maxImagesCount:1 isCheckBody:YES photosBlock:^(NSArray * _Nonnull imagesArr) {
  92. if (imagesArr.count > 0) {
  93. [self getImageStrWithImage:imagesArr.firstObject completeBlock:^(NSString *backString) {
  94. if (self.checkBodyCompletedBlock && RQStringIsNotEmpty(backString)) {
  95. self.checkBodyCompletedBlock(YES, @{@"normalImg" : backString, @"actionImg" : backString});
  96. }
  97. }];
  98. } else {
  99. if (self.checkBodyCompletedBlock) {
  100. self.checkBodyCompletedBlock(NO, nil);
  101. }
  102. }
  103. }];
  104. } else {
  105. BDFaceLivenessViewController* lvc = [[BDFaceLivenessViewController alloc] init];
  106. [[IDLFaceLivenessManager sharedInstance] reset];
  107. [[BDFaceImageShow sharedInstance] reset];
  108. NSMutableArray *liveActionBeginArray = self.allLiveActionArr.mutableCopy;
  109. NSMutableArray *liveActionEndArray = @[].mutableCopy;
  110. if (num != 1) {
  111. [liveActionBeginArray removeObject:@(FaceLivenessActionTypeLiveMouth)];
  112. [liveActionEndArray addObject:@(FaceLivenessActionTypeLiveMouth)];
  113. NSInteger a = num - 1;
  114. num = (a >= 0)? a : 0;
  115. }
  116. NSInteger b = liveActionBeginArray.count;
  117. for (int i = 0; i < ((num > b)? b : num); i++) {
  118. int r = arc4random() % [liveActionBeginArray count];
  119. [liveActionEndArray addObject:liveActionBeginArray[r]];
  120. [liveActionBeginArray removeObject:liveActionBeginArray[r]];
  121. }
  122. BDFaceLivingConfigModel *faceLivingConfigModel = [BDFaceLivingConfigModel sharedInstance];
  123. if (liveActionEndArray.count > 0) {
  124. faceLivingConfigModel.liveActionArray = liveActionEndArray;
  125. faceLivingConfigModel.numOfLiveness = liveActionEndArray.count;
  126. faceLivingConfigModel.isByOrder = YES;
  127. }
  128. [lvc livenesswithList:faceLivingConfigModel.liveActionArray order:faceLivingConfigModel.isByOrder numberOfLiveness:faceLivingConfigModel.numOfLiveness];
  129. [lvc initWithCompleteBlock:^(BOOL success) {
  130. @strongify(self)
  131. if (success) {
  132. [self getImageStrWithImage:[[BDFaceImageShow sharedInstance] getSuccessImage] completeBlock:^(NSString *backString) {
  133. if (self.checkBodyCompletedBlock && RQStringIsNotEmpty(backString)) {
  134. self.checkBodyCompletedBlock(YES, @{@"normalImg" : backString, @"actionImg" : [RQ_CHECKBODY_MANAGER getActionImageString]});
  135. }
  136. }];
  137. } else {
  138. if (self.checkBodyCompletedBlock) {
  139. self.checkBodyCompletedBlock(NO, nil);
  140. }
  141. }
  142. }];
  143. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:lvc];
  144. navi.modalPresentationStyle = UIModalPresentationFullScreen;
  145. [QMUIHelper.visibleViewController presentViewController:navi animated:YES completion:nil];
  146. }
  147. }
  148. /// 设置活体动作图片base64字符串
  149. /// @param actionImageString 活体动作图片base64字符串
  150. - (void)setActionImageString:(NSString *)actionImageString {
  151. _actionImageString = actionImageString;
  152. }
  153. /// 获取活体动作图片base64字符串
  154. - (NSString *)getActionImageString{
  155. return self.actionImageString? : @"";
  156. }
  157. - (void)getImageStrWithImage:(UIImage *)image completeBlock:(void(^)(NSString *backString))completeBlock {
  158. @weakify(self)
  159. UIImage *normalImage = [UIImage fixOrientation:image];
  160. CGSize imageSize = CGSizeMake(260, 378);
  161. UIImage *newImage;
  162. switch (self.checkBodyType) {
  163. case RQCheckBodyType_Default:
  164. imageSize = CGSizeMake(260, 378);
  165. newImage = [normalImage qmui_imageResizedInLimitedSize:imageSize];
  166. break;
  167. case RQCheckBodyType_PTSC: {
  168. CGFloat width = [[RQ_COMMON_MANAGER.JSJP_APP_PTSC_FBL componentsSeparatedByString:@","].firstObject floatValue];
  169. CGFloat height = [[RQ_COMMON_MANAGER.JSJP_APP_PTSC_FBL componentsSeparatedByString:@","].lastObject floatValue];
  170. imageSize = CGSizeMake(width, height);
  171. newImage = [normalImage qmui_imageWithClippedRect:CGRectMake(0, normalImage.size.width / 4.f, normalImage.size.width, normalImage.size.width)];
  172. newImage = [newImage qmui_imageResizedInLimitedSize:imageSize resizingMode:QMUIImageResizingModeScaleAspectFill];
  173. // imageSize = CGSizeMake(320, 240);
  174. // newImage = [normalImage qmui_imageWithClippedRect:CGRectMake(0, normalImage.size.width / 4.f, normalImage.size.width, normalImage.size.width)];
  175. // CGFloat a = (normalImage.size.width * (32 / 24) - normalImage.size.width) / 2.f;
  176. // newImage = [newImage qmui_imageWithSpacingExtensionInsets:UIEdgeInsetsMake(0, a, 0, - a)];
  177. // newImage = [newImage qmui_imageResizedInLimitedSize:imageSize resizingMode:QMUIImageResizingModeScaleAspectFill];
  178. break;
  179. }
  180. default:
  181. break;
  182. }
  183. if (!RQObjectIsNil(self.waterMarkArr) && self.waterMarkArr.count > 1) {
  184. self.waterImage = newImage;
  185. __block NSUInteger myIndex = 0;
  186. [[self.waterMarkArr.rac_sequence.signal deliverOnMainThread] subscribeNext:^(NSString *waterMarkStr) {
  187. @strongify(self)
  188. self.index = myIndex;
  189. CGPoint point = CGPointZero;
  190. CGFloat strHeight = [waterMarkStr heightForFont:RQRegularFont(20) width:RQ_SCREEN_WIDTH - 32];
  191. if (self.index < 4) {
  192. CGFloat spaceHeight = (self.index > 0)? 8 : 0;
  193. point = CGPointMake(16, 16 + self.index * strHeight + (self.index * spaceHeight));
  194. } else {
  195. CGFloat spaceHeight = (self.index == 4)? 0 : 8;
  196. point = CGPointMake(16, newImage.size.height - 16 - (strHeight * (self.index - 3)) - (spaceHeight * (self.index - 4)));
  197. }
  198. self.waterImage = [self.waterImage imageWaterMarkWithString:waterMarkStr point:point attribute:@{NSForegroundColorAttributeName:UIColor.whiteColor,NSFontAttributeName:RQRegularFont(20)}];
  199. myIndex ++;
  200. } completed:^{
  201. NSData *imgData = [UIImage resetSizeOfImageData:self.waterImage maxSize:200];
  202. NSString *backString = [NSString stringWithFormat:@"%@",[imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
  203. if (completeBlock) {
  204. completeBlock(backString);
  205. }
  206. }];
  207. } else {
  208. NSData *imgData = [UIImage resetSizeOfImageData:newImage maxSize:200];
  209. NSString *backString = [NSString stringWithFormat:@"%@",[imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
  210. if (completeBlock) {
  211. completeBlock(backString);
  212. }
  213. }
  214. }
  215. - (NSArray *)allLiveActionArr {
  216. return @[@(FaceLivenessActionTypeLiveEye),
  217. @(FaceLivenessActionTypeLiveMouth),
  218. @(FaceLivenessActionTypeLiveYawRight),
  219. @(FaceLivenessActionTypeLiveYawLeft),
  220. @(FaceLivenessActionTypeLivePitchUp),
  221. @(FaceLivenessActionTypeLivePitchDown),
  222. ];
  223. }
  224. @end