CheckBodyManager.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. // 读取设置配置,启动活体检测与否
  52. NSNumber *LiveMode = [[NSUserDefaults standardUserDefaults] objectForKey:@"LiveMode"];
  53. if (LiveMode.boolValue){
  54. [self faceLivenessWithCheckNum:num];
  55. } else {
  56. [self faceDetect];
  57. }
  58. }
  59. - (void)faceDetect {
  60. @weakify(self)
  61. BDFaceDetectionViewController* dvc = [[BDFaceDetectionViewController alloc] init];
  62. [dvc initWithCompleteBlock:^(BOOL success) {
  63. @strongify(self)
  64. if (success) {
  65. [self getImageStrWithImage:[[BDFaceImageShow sharedInstance] getSuccessImage] completeBlock:^(NSString *backString) {
  66. if (self.checkBodyCompletedBlock && RQStringIsNotEmpty(backString)) {
  67. self.checkBodyCompletedBlock(YES, @{@"normalImg" : backString, @"actionImg" : [RQ_CHECKBODY_MANAGER getActionImageString]});
  68. }
  69. }];
  70. } else {
  71. if (self.checkBodyCompletedBlock) {
  72. self.checkBodyCompletedBlock(NO, nil);
  73. }
  74. }
  75. [[IDLFaceLivenessManager sharedInstance] reset];
  76. [[BDFaceImageShow sharedInstance] reset];
  77. }];
  78. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:dvc];
  79. navi.navigationBarHidden = true;
  80. navi.modalPresentationStyle = UIModalPresentationFullScreen;
  81. [QMUIHelper.visibleViewController presentViewController:navi animated:YES completion:nil];
  82. }
  83. - (void)faceLivenessWithCheckNum:(NSInteger)num {
  84. @weakify(self)
  85. if (num == 0) {
  86. [RQ_SHARE_FUNCTION getPhotosWithGetPhotosWay:GetPhotosWay_Camera size:kSize maxLength:200 maxImagesCount:1 isCheckBody:YES photosBlock:^(NSArray * _Nonnull imagesArr) {
  87. if (imagesArr.count > 0) {
  88. [self getImageStrWithImage:imagesArr.firstObject completeBlock:^(NSString *backString) {
  89. if (self.checkBodyCompletedBlock && RQStringIsNotEmpty(backString)) {
  90. self.checkBodyCompletedBlock(YES, @{@"normalImg" : backString, @"actionImg" : backString});
  91. }
  92. }];
  93. } else {
  94. if (self.checkBodyCompletedBlock) {
  95. self.checkBodyCompletedBlock(NO, nil);
  96. }
  97. }
  98. }];
  99. } else {
  100. BDFaceLivenessViewController* lvc = [[BDFaceLivenessViewController alloc] init];
  101. [[IDLFaceLivenessManager sharedInstance] reset];
  102. [[BDFaceImageShow sharedInstance] reset];
  103. NSMutableArray *liveActionBeginArray = self.allLiveActionArr.mutableCopy;
  104. NSMutableArray *liveActionEndArray = @[].mutableCopy;
  105. if (num != 1) {
  106. [liveActionBeginArray removeObject:@(FaceLivenessActionTypeLiveMouth)];
  107. [liveActionEndArray addObject:@(FaceLivenessActionTypeLiveMouth)];
  108. NSInteger a = num - 1;
  109. num = (a >= 0)? a : 0;
  110. }
  111. NSInteger b = liveActionBeginArray.count;
  112. for (int i = 0; i < ((num > b)? b : num); i++) {
  113. int r = arc4random() % [liveActionBeginArray count];
  114. [liveActionEndArray addObject:liveActionBeginArray[r]];
  115. [liveActionBeginArray removeObject:liveActionBeginArray[r]];
  116. }
  117. BDFaceLivingConfigModel *faceLivingConfigModel = [BDFaceLivingConfigModel sharedInstance];
  118. if (liveActionEndArray.count > 0) {
  119. faceLivingConfigModel.liveActionArray = liveActionEndArray;
  120. faceLivingConfigModel.numOfLiveness = liveActionEndArray.count;
  121. faceLivingConfigModel.isByOrder = YES;
  122. }
  123. [lvc livenesswithList:faceLivingConfigModel.liveActionArray order:faceLivingConfigModel.isByOrder numberOfLiveness:faceLivingConfigModel.numOfLiveness];
  124. [lvc initWithCompleteBlock:^(BOOL success) {
  125. @strongify(self)
  126. if (success) {
  127. [self getImageStrWithImage:[[BDFaceImageShow sharedInstance] getSuccessImage] completeBlock:^(NSString *backString) {
  128. if (self.checkBodyCompletedBlock && RQStringIsNotEmpty(backString)) {
  129. self.checkBodyCompletedBlock(YES, @{@"normalImg" : backString, @"actionImg" : [RQ_CHECKBODY_MANAGER getActionImageString]});
  130. }
  131. }];
  132. } else {
  133. if (self.checkBodyCompletedBlock) {
  134. self.checkBodyCompletedBlock(NO, nil);
  135. }
  136. }
  137. }];
  138. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:lvc];
  139. navi.modalPresentationStyle = UIModalPresentationFullScreen;
  140. [QMUIHelper.visibleViewController presentViewController:navi animated:YES completion:nil];
  141. }
  142. }
  143. /// 设置活体动作图片base64字符串
  144. /// @param actionImageString 活体动作图片base64字符串
  145. - (void)setActionImageString:(NSString *)actionImageString {
  146. _actionImageString = actionImageString;
  147. }
  148. /// 获取活体动作图片base64字符串
  149. - (NSString *)getActionImageString{
  150. return self.actionImageString? : @"";
  151. }
  152. - (void)getImageStrWithImage:(UIImage *)image completeBlock:(void(^)(NSString *backString))completeBlock {
  153. @weakify(self)
  154. UIImage *normalImage = [UIImage fixOrientation:image];
  155. CGSize imageSize = CGSizeMake(260, 378);
  156. UIImage *newImage;
  157. switch (self.checkBodyType) {
  158. case RQCheckBodyType_Default:
  159. imageSize = CGSizeMake(260, 378);
  160. newImage = [normalImage qmui_imageResizedInLimitedSize:imageSize];
  161. break;
  162. case RQCheckBodyType_PTSC: {
  163. CGFloat width = [[RQ_COMMON_MANAGER.JSJP_APP_PTSC_FBL componentsSeparatedByString:@","].firstObject floatValue];
  164. CGFloat height = [[RQ_COMMON_MANAGER.JSJP_APP_PTSC_FBL componentsSeparatedByString:@","].lastObject floatValue];
  165. imageSize = CGSizeMake(width, height);
  166. newImage = [normalImage qmui_imageWithClippedRect:CGRectMake(0, normalImage.size.width / 4.f, normalImage.size.width, normalImage.size.width)];
  167. newImage = [newImage qmui_imageResizedInLimitedSize:imageSize resizingMode:QMUIImageResizingModeScaleAspectFill];
  168. // imageSize = CGSizeMake(320, 240);
  169. // newImage = [normalImage qmui_imageWithClippedRect:CGRectMake(0, normalImage.size.width / 4.f, normalImage.size.width, normalImage.size.width)];
  170. // CGFloat a = (normalImage.size.width * (32 / 24) - normalImage.size.width) / 2.f;
  171. // newImage = [newImage qmui_imageWithSpacingExtensionInsets:UIEdgeInsetsMake(0, a, 0, - a)];
  172. // newImage = [newImage qmui_imageResizedInLimitedSize:imageSize resizingMode:QMUIImageResizingModeScaleAspectFill];
  173. break;
  174. }
  175. default:
  176. break;
  177. }
  178. if (!RQObjectIsNil(self.waterMarkArr) && self.waterMarkArr.count > 1) {
  179. self.waterImage = newImage;
  180. __block NSUInteger myIndex = 0;
  181. [[self.waterMarkArr.rac_sequence.signal deliverOnMainThread] subscribeNext:^(NSString *waterMarkStr) {
  182. @strongify(self)
  183. self.index = myIndex;
  184. CGPoint point = CGPointZero;
  185. CGFloat strHeight = [waterMarkStr heightForFont:RQRegularFont(20) width:RQ_SCREEN_WIDTH - 32];
  186. if (self.index < 4) {
  187. CGFloat spaceHeight = (self.index > 0)? 8 : 0;
  188. point = CGPointMake(16, 16 + self.index * strHeight + (self.index * spaceHeight));
  189. } else {
  190. CGFloat spaceHeight = (self.index == 4)? 0 : 8;
  191. point = CGPointMake(16, newImage.size.height - 16 - (strHeight * (self.index - 3)) - (spaceHeight * (self.index - 4)));
  192. }
  193. self.waterImage = [self.waterImage imageWaterMarkWithString:waterMarkStr point:point attribute:@{NSForegroundColorAttributeName:UIColor.whiteColor,NSFontAttributeName:RQRegularFont(20)}];
  194. myIndex ++;
  195. } completed:^{
  196. NSData *imgData = [UIImage resetSizeOfImageData:self.waterImage maxSize:200];
  197. NSString *backString = [NSString stringWithFormat:@"%@",[imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
  198. if (completeBlock) {
  199. completeBlock(backString);
  200. }
  201. }];
  202. } else {
  203. NSData *imgData = [UIImage resetSizeOfImageData:newImage maxSize:200];
  204. NSString *backString = [NSString stringWithFormat:@"%@",[imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
  205. if (completeBlock) {
  206. completeBlock(backString);
  207. }
  208. }
  209. }
  210. - (NSArray *)allLiveActionArr {
  211. return @[@(FaceLivenessActionTypeLiveEye),
  212. @(FaceLivenessActionTypeLiveMouth),
  213. @(FaceLivenessActionTypeLiveYawRight),
  214. @(FaceLivenessActionTypeLiveYawLeft),
  215. @(FaceLivenessActionTypeLivePitchUp),
  216. @(FaceLivenessActionTypeLivePitchDown),
  217. ];
  218. }
  219. @end