CheckBodyManager.m 9.7 KB

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