123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- //
- // 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
|