BDFaceDetectionViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // DetectionViewController.m
  3. // FaceSDKSample_IOS
  4. //
  5. // Created by 阿凡树 on 2017/5/23.
  6. // Copyright © 2017年 Baidu. All rights reserved.
  7. //
  8. #import "BDFaceDetectionViewController.h"
  9. #import <IDLFaceSDK/IDLFaceSDK.h>
  10. #import <AVFoundation/AVFoundation.h>
  11. #import "BDFaceSuccessViewController.h"
  12. #import "BDFaceImageShow.h"
  13. @interface BDFaceDetectionViewController ()
  14. {
  15. UIImageView * newImage;
  16. BOOL isPaint;
  17. }
  18. @property (nonatomic, readwrite, retain) UIView *animaView;
  19. @end
  20. int remindCode = -1;
  21. @implementation BDFaceDetectionViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. // 纯粹为了在照片成功之后,做闪屏幕动画之用
  25. self.animaView = [[UIView alloc] initWithFrame:self.view.bounds];
  26. self.animaView.backgroundColor = [UIColor whiteColor];
  27. self.animaView.alpha = 0;
  28. [self.view addSubview:self.animaView];
  29. }
  30. - (void)viewWillAppear:(BOOL)animated {
  31. [super viewWillAppear:animated];
  32. NSLog(@"进入新的界面");
  33. [[IDLFaceDetectionManager sharedInstance] startInitial];
  34. }
  35. - (void)viewDidDisappear:(BOOL)animated {
  36. [super viewDidDisappear:animated];
  37. [[IDLFaceDetectionManager sharedInstance] reset];
  38. }
  39. - (void)onAppWillResignAction {
  40. [super onAppWillResignAction];
  41. [IDLFaceDetectionManager.sharedInstance reset];
  42. }
  43. - (void)didReceiveMemoryWarning {
  44. [super didReceiveMemoryWarning];
  45. // Dispose of any resources that can be recreated.
  46. }
  47. - (void)faceProcesss:(UIImage *)image {
  48. if (self.hasFinished) {
  49. return;
  50. }
  51. __weak typeof(self) weakSelf = self;
  52. [[IDLFaceDetectionManager sharedInstance] detectStratrgyWithNormalImage:image previewRect:self.previewRect detectRect:self.detectRect completionHandler:^(FaceInfo *faceinfo, NSDictionary *images, DetectRemindCode remindCode) {
  53. /*
  54. 此注释里的代码用于显示人脸框,调试过程中需要显示人脸款可打开注释
  55. // 绘制人脸框功能,开发者可以通过观察人脸框_faceRectFit 在 previewRect 包含关系判断是框内还是框外
  56. dispatch_async(dispatch_get_main_queue(), ^{
  57. CGRect faceRect = [BDFaceQualityUtil getFaceRect:faceinfo.landMarks withCount:faceinfo.landMarks.count];
  58. CGRect faceRectFit = [BDFaceUtil convertRectFrom:faceRect image:image previewRect:previewRect];
  59. if (!isPaint) {
  60. newImage= [[UIImageView alloc]init];
  61. [self.view addSubview:newImage];
  62. isPaint = !isPaint;
  63. }
  64. newImage = [self creatRectangle:newImage withRect:faceRectFit withcolor:[UIColor blackColor]];
  65. });
  66. */
  67. switch (remindCode) {
  68. case DetectRemindCodeOK: {
  69. weakSelf.hasFinished = YES;
  70. [self warningStatus:CommonStatus warning:@"非常好"];
  71. if (images[@"image"] != nil && [images[@"image"] count] != 0) {
  72. NSArray *imageArr = images[@"image"];
  73. // for (FaceCropImageInfo * image in imageArr) {
  74. // NSLog(@"cropImageWithBlack %f %f", image.cropImageWithBlack.size.height, image.cropImageWithBlack.size.width);
  75. // NSLog(@"originalImage %f %f", image.originalImage.size.height, image.originalImage.size.width);
  76. // }
  77. NSString * key = [NSString stringWithFormat:@"%@",images.allKeys[((defUser.ycbdFaceCount - 1) >=0 )? (defUser.ycbdFaceCount - 1) : 0]];
  78. NSString * base64Str = images[key];
  79. [RQ_CHECKBODY_MANAGER setActionImageString:base64Str];
  80. FaceCropImageInfo * bestImage = imageArr[0];
  81. // UI显示选择原图,避免黑框情况
  82. [[BDFaceImageShow sharedInstance] setSuccessImage:bestImage.originalImage];
  83. [[BDFaceImageShow sharedInstance] setSilentliveScore:bestImage.silentliveScore];
  84. // 公安验证接口测试,网络接口上传选择扣图,避免占用贷款
  85. // [self request:bestImage.cropImageWithBlackEncryptStr];
  86. dispatch_async(dispatch_get_main_queue(), ^{
  87. // UIViewController* fatherViewController = weakSelf.presentingViewController;
  88. // [weakSelf dismissViewControllerAnimated:YES completion:^{
  89. // BDFaceSuccessViewController *avc = [[BDFaceSuccessViewController alloc] init];
  90. // avc.modalPresentationStyle = UIModalPresentationFullScreen;
  91. // [fatherViewController presentViewController:avc animated:YES completion:nil];
  92. // [self closeAction];
  93. // }];
  94. [self closeActionWithIsSuccess:YES];
  95. });
  96. }
  97. [self singleActionSuccess:true];
  98. break;
  99. }
  100. case DetectRemindCodeDataHitOne:
  101. [self warningStatus:CommonStatus warning:@"非常好"];
  102. break;
  103. case DetectRemindCodePitchOutofDownRange:
  104. [self warningStatus:PoseStatus warning:@"请略微抬头"];
  105. [self singleActionSuccess:false];
  106. break;
  107. case DetectRemindCodePitchOutofUpRange:
  108. [self warningStatus:PoseStatus warning:@"请略微低头"];
  109. [self singleActionSuccess:false];
  110. break;
  111. case DetectRemindCodeYawOutofLeftRange:
  112. [self warningStatus:PoseStatus warning:@"请略微向右转头"];
  113. [self singleActionSuccess:false];
  114. break;
  115. case DetectRemindCodeYawOutofRightRange:
  116. [self warningStatus:PoseStatus warning:@"请略微向左转头"];
  117. [self singleActionSuccess:false];
  118. break;
  119. case DetectRemindCodePoorIllumination:
  120. [self warningStatus:CommonStatus warning:@"请使环境光线再亮些"];
  121. [self singleActionSuccess:false];
  122. break;
  123. case DetectRemindCodeNoFaceDetected:
  124. [self warningStatus:CommonStatus warning:@"把脸移入框内"];
  125. [self singleActionSuccess:false];
  126. break;
  127. case DetectRemindCodeImageBlured:
  128. [self warningStatus:PoseStatus warning:@"请握稳手机"];
  129. [self singleActionSuccess:false];
  130. break;
  131. case DetectRemindCodeOcclusionLeftEye:
  132. [self warningStatus:occlusionStatus warning:@"左眼有遮挡"];
  133. [self singleActionSuccess:false];
  134. break;
  135. case DetectRemindCodeOcclusionRightEye:
  136. [self warningStatus:occlusionStatus warning:@"右眼有遮挡"];
  137. [self singleActionSuccess:false];
  138. break;
  139. case DetectRemindCodeOcclusionNose:
  140. [self warningStatus:occlusionStatus warning:@"鼻子有遮挡"];
  141. [self singleActionSuccess:false];
  142. break;
  143. case DetectRemindCodeOcclusionMouth:
  144. [self warningStatus:occlusionStatus warning:@"嘴巴有遮挡"];
  145. [self singleActionSuccess:false];
  146. break;
  147. case DetectRemindCodeOcclusionLeftContour:
  148. [self warningStatus:occlusionStatus warning:@"左脸颊有遮挡"];
  149. [self singleActionSuccess:false];
  150. break;
  151. case DetectRemindCodeOcclusionRightContour:
  152. [self warningStatus:occlusionStatus warning:@"右脸颊有遮挡"];
  153. [self singleActionSuccess:false];
  154. break;
  155. case DetectRemindCodeOcclusionChinCoutour:
  156. [self warningStatus:occlusionStatus warning:@"下颚有遮挡"];
  157. [self singleActionSuccess:false];
  158. break;
  159. case DetectRemindCodeTooClose:
  160. [self warningStatus:CommonStatus warning:@"请将脸部离远一点"];
  161. [self singleActionSuccess:false];
  162. break;
  163. case DetectRemindCodeTooFar:
  164. [self warningStatus:CommonStatus warning:@"请将脸部靠近一点"];
  165. [self singleActionSuccess:false];
  166. break;
  167. case DetectRemindCodeBeyondPreviewFrame:
  168. [self warningStatus:CommonStatus warning:@"把脸移入框内"];
  169. [self singleActionSuccess:false];
  170. break;
  171. case DetectRemindCodeVerifyInitError:
  172. [self warningStatus:CommonStatus warning:@"验证失败"];
  173. break;
  174. case DetectRemindCodeTimeout: {
  175. // 时间超时,重置之前采集数据
  176. [[IDLFaceDetectionManager sharedInstance] reset];
  177. dispatch_async(dispatch_get_main_queue(), ^{
  178. [self isTimeOut:YES];
  179. });
  180. break;
  181. }
  182. case DetectRemindCodeConditionMeet: {
  183. }
  184. break;
  185. default:
  186. break;
  187. }
  188. }];
  189. }
  190. -(void) saveImage:(UIImage *) image withFileName:(NSString *) fileName{
  191. NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  192. // 2.创建一个文件路径
  193. NSString *filePath = [docPath stringByAppendingPathComponent:fileName];
  194. // 3.创建文件首先需要一个文件管理对象
  195. NSFileManager *fileManager = [NSFileManager defaultManager];
  196. // 4.创建文件
  197. [fileManager createFileAtPath:filePath contents:nil attributes:nil];
  198. NSError * error = nil;
  199. BOOL written = [UIImageJPEGRepresentation(image,1.0f) writeToFile:filePath options:0 error:&error];
  200. if(!written){
  201. NSLog(@"write failed %@", [error localizedDescription]);
  202. }
  203. }
  204. -(void) saveFile:(NSString *) fileName withContent:(NSString *) content{
  205. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  206. NSString *homePath = [paths objectAtIndex:0];
  207. NSString *filePath = [homePath stringByAppendingPathComponent:fileName];
  208. NSFileManager *fileManager = [NSFileManager defaultManager];
  209. if(![fileManager fileExistsAtPath:filePath]) //如果不存在
  210. {
  211. NSString *str = @"索引 是否活体 活体分值 活体图片路径\n";
  212. [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  213. }
  214. NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
  215. [fileHandle seekToEndOfFile]; //将节点跳到文件的末尾
  216. NSData* stringData = [content dataUsingEncoding:NSUTF8StringEncoding];
  217. [fileHandle writeData:stringData]; //追加写入数据
  218. [fileHandle closeFile];
  219. }
  220. -(NSString *) getCurrentTimes{
  221. NSDateFormatter *formatter = [NSDateFormatter rq_defaultDateFormatter];
  222. // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
  223. //现在时间,你可以输出来看下是什么格式
  224. NSDate *datenow = [NSDate date];
  225. //----------将nsdate按formatter格式转成nsstring
  226. NSString *currentTimeString = [formatter stringFromDate:datenow];
  227. return currentTimeString;
  228. }
  229. - (void) request:(NSString *) imageStr{
  230. NSError *error;
  231. NSString *urlString = @"http://10.24.7.251:8316/api/v3/person/verify_sec?appid=7758258";
  232. NSURL *url = [NSURL URLWithString:urlString];
  233. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  234. NSMutableDictionary * dictionary = [[NSMutableDictionary alloc] init];
  235. dictionary[@"risk_identify"] = @(false);
  236. dictionary[@"image_type"] = @"BASE64";
  237. dictionary[@"image"] = imageStr;
  238. dictionary[@"id_card_number"] = @"请输入你的身份证";
  239. dictionary[@"name"] = @"请输入你的姓名";
  240. dictionary[@"quality_control"] = @"NONE";
  241. dictionary[@"liveness_control"] = @"NONE";
  242. dictionary[@"risk_identify"] = @YES;
  243. dictionary[@"zid"] = [[FaceSDKManager sharedInstance] getZtoken];
  244. dictionary[@"ip"] = @"172.30.154.173";
  245. dictionary[@"phone"] = @"18610317119";
  246. dictionary[@"image_sec"] = @NO;
  247. dictionary[@"app"] = @"ios";
  248. dictionary[@"ev"] = @"smrz";
  249. NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
  250. [request setHTTPMethod:@"POST"];
  251. [request setURL:url];
  252. [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  253. [request setHTTPBody:jsonData];
  254. NSData *finalDataToDisplay = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
  255. NSMutableDictionary *abc = [NSJSONSerialization JSONObjectWithData: finalDataToDisplay
  256. options: NSJSONReadingMutableContainers
  257. error: &error];
  258. NSLog(@"%@", abc);
  259. }
  260. - (void)selfReplayFunction{
  261. [[IDLFaceDetectionManager sharedInstance] reset];
  262. }
  263. @end