BDFaceDetectionViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 && [(NSArray *)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[((RQ_USER_MANAGER.ycbdFaceCount - 1) >=0 )? (RQ_USER_MANAGER.ycbdFaceCount - 1) : 0]];
  78. NSString * base64Str = images[key];
  79. // NSData * showData = [[NSData alloc]initWithBase64EncodedString:base64Str options:NSDataBase64DecodingIgnoreUnknownCharacters];
  80. // UIImage *myImage = [UIImage imageWithData:showData];
  81. [RQ_CHECKBODY_MANAGER setActionImageString:base64Str];
  82. FaceCropImageInfo * bestImage = imageArr[0];
  83. // UI显示选择原图,避免黑框情况
  84. [[BDFaceImageShow sharedInstance] setSuccessImage:bestImage.originalImage];
  85. [[BDFaceImageShow sharedInstance] setSilentliveScore:bestImage.silentliveScore];
  86. // 公安验证接口测试,网络接口上传选择扣图,避免占用贷款
  87. // [self request:bestImage.cropImageWithBlackEncryptStr];
  88. dispatch_async(dispatch_get_main_queue(), ^{
  89. // UIViewController* fatherViewController = weakSelf.presentingViewController;
  90. // [weakSelf dismissViewControllerAnimated:YES completion:^{
  91. // BDFaceSuccessViewController *avc = [[BDFaceSuccessViewController alloc] init];
  92. // avc.modalPresentationStyle = UIModalPresentationFullScreen;
  93. // [fatherViewController presentViewController:avc animated:YES completion:nil];
  94. // [self closeAction];
  95. // }];
  96. [self closeActionWithIsSuccess:YES];
  97. });
  98. }
  99. [self singleActionSuccess:true];
  100. break;
  101. }
  102. case DetectRemindCodeDataHitOne:
  103. [self warningStatus:CommonStatus warning:@"非常好"];
  104. break;
  105. case DetectRemindCodePitchOutofDownRange:
  106. [self warningStatus:PoseStatus warning:@"请略微抬头"];
  107. [self singleActionSuccess:false];
  108. break;
  109. case DetectRemindCodePitchOutofUpRange:
  110. [self warningStatus:PoseStatus warning:@"请略微低头"];
  111. [self singleActionSuccess:false];
  112. break;
  113. case DetectRemindCodeYawOutofLeftRange:
  114. [self warningStatus:PoseStatus warning:@"请略微向右转头"];
  115. [self singleActionSuccess:false];
  116. break;
  117. case DetectRemindCodeYawOutofRightRange:
  118. [self warningStatus:PoseStatus warning:@"请略微向左转头"];
  119. [self singleActionSuccess:false];
  120. break;
  121. case DetectRemindCodePoorIllumination:
  122. [self warningStatus:CommonStatus warning:@"请使环境光线再亮些"];
  123. [self singleActionSuccess:false];
  124. break;
  125. case DetectRemindCodeNoFaceDetected:
  126. [self warningStatus:CommonStatus warning:@"把脸移入框内"];
  127. [self singleActionSuccess:false];
  128. break;
  129. case DetectRemindCodeImageBlured:
  130. [self warningStatus:PoseStatus warning:@"请握稳手机"];
  131. [self singleActionSuccess:false];
  132. break;
  133. case DetectRemindCodeOcclusionLeftEye:
  134. [self warningStatus:occlusionStatus warning:@"左眼有遮挡"];
  135. [self singleActionSuccess:false];
  136. break;
  137. case DetectRemindCodeOcclusionRightEye:
  138. [self warningStatus:occlusionStatus warning:@"右眼有遮挡"];
  139. [self singleActionSuccess:false];
  140. break;
  141. case DetectRemindCodeOcclusionNose:
  142. [self warningStatus:occlusionStatus warning:@"鼻子有遮挡"];
  143. [self singleActionSuccess:false];
  144. break;
  145. case DetectRemindCodeOcclusionMouth:
  146. [self warningStatus:occlusionStatus warning:@"嘴巴有遮挡"];
  147. [self singleActionSuccess:false];
  148. break;
  149. case DetectRemindCodeOcclusionLeftContour:
  150. [self warningStatus:occlusionStatus warning:@"左脸颊有遮挡"];
  151. [self singleActionSuccess:false];
  152. break;
  153. case DetectRemindCodeOcclusionRightContour:
  154. [self warningStatus:occlusionStatus warning:@"右脸颊有遮挡"];
  155. [self singleActionSuccess:false];
  156. break;
  157. case DetectRemindCodeOcclusionChinCoutour:
  158. [self warningStatus:occlusionStatus warning:@"下颚有遮挡"];
  159. [self singleActionSuccess:false];
  160. break;
  161. case DetectRemindCodeTooClose:
  162. [self warningStatus:CommonStatus warning:@"请将脸部离远一点"];
  163. [self singleActionSuccess:false];
  164. break;
  165. case DetectRemindCodeTooFar:
  166. [self warningStatus:CommonStatus warning:@"请将脸部靠近一点"];
  167. [self singleActionSuccess:false];
  168. break;
  169. case DetectRemindCodeBeyondPreviewFrame:
  170. [self warningStatus:CommonStatus warning:@"把脸移入框内"];
  171. [self singleActionSuccess:false];
  172. break;
  173. case DetectRemindCodeVerifyInitError:
  174. [self warningStatus:CommonStatus warning:@"验证失败"];
  175. break;
  176. case DetectRemindCodeTimeout: {
  177. // 时间超时,重置之前采集数据
  178. [[IDLFaceDetectionManager sharedInstance] reset];
  179. dispatch_async(dispatch_get_main_queue(), ^{
  180. [self isTimeOut:YES];
  181. });
  182. break;
  183. }
  184. case DetectRemindCodeConditionMeet: {
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. }];
  191. }
  192. -(void) saveImage:(UIImage *) image withFileName:(NSString *) fileName{
  193. NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  194. // 2.创建一个文件路径
  195. NSString *filePath = [docPath stringByAppendingPathComponent:fileName];
  196. // 3.创建文件首先需要一个文件管理对象
  197. NSFileManager *fileManager = [NSFileManager defaultManager];
  198. // 4.创建文件
  199. [fileManager createFileAtPath:filePath contents:nil attributes:nil];
  200. NSError * error = nil;
  201. BOOL written = [UIImageJPEGRepresentation(image,1.0f) writeToFile:filePath options:0 error:&error];
  202. if(!written){
  203. NSLog(@"write failed %@", [error localizedDescription]);
  204. }
  205. }
  206. -(void) saveFile:(NSString *) fileName withContent:(NSString *) content{
  207. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  208. NSString *homePath = [paths objectAtIndex:0];
  209. NSString *filePath = [homePath stringByAppendingPathComponent:fileName];
  210. NSFileManager *fileManager = [NSFileManager defaultManager];
  211. if(![fileManager fileExistsAtPath:filePath]) //如果不存在
  212. {
  213. NSString *str = @"索引 是否活体 活体分值 活体图片路径\n";
  214. [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  215. }
  216. NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
  217. [fileHandle seekToEndOfFile]; //将节点跳到文件的末尾
  218. NSData* stringData = [content dataUsingEncoding:NSUTF8StringEncoding];
  219. [fileHandle writeData:stringData]; //追加写入数据
  220. [fileHandle closeFile];
  221. }
  222. -(NSString *) getCurrentTimes{
  223. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  224. formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
  225. // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
  226. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  227. //现在时间,你可以输出来看下是什么格式
  228. NSDate *datenow = [NSDate date];
  229. //----------将nsdate按formatter格式转成nsstring
  230. NSString *currentTimeString = [formatter stringFromDate:datenow];
  231. return currentTimeString;
  232. }
  233. //- (void) request:(NSString *) imageStr{
  234. // NSError *error;
  235. // NSString *urlString = @"http://10.24.7.251:8316/api/v3/person/verify_sec?appid=7758258";
  236. // NSURL *url = [NSURL URLWithString:urlString];
  237. // NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  238. // NSMutableDictionary * dictionary = [[NSMutableDictionary alloc] init];
  239. // dictionary[@"risk_identify"] = @(false);
  240. //
  241. //
  242. // dictionary[@"image_type"] = @"BASE64";
  243. // dictionary[@"image"] = imageStr;
  244. // dictionary[@"id_card_number"] = @"请输入你的身份证";
  245. // dictionary[@"name"] = @"请输入你的姓名";
  246. // dictionary[@"quality_control"] = @"NONE";
  247. // dictionary[@"liveness_control"] = @"NONE";
  248. // dictionary[@"risk_identify"] = @YES;
  249. // dictionary[@"zid"] = [[FaceSDKManager sharedInstance] getZtoken];
  250. // dictionary[@"ip"] = @"172.30.154.173";
  251. // dictionary[@"phone"] = @"18610317119";
  252. // dictionary[@"image_sec"] = @NO;
  253. // dictionary[@"app"] = @"ios";
  254. // dictionary[@"ev"] = @"smrz";
  255. // NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
  256. // [request setHTTPMethod:@"POST"];
  257. // [request setURL:url];
  258. // [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  259. // [request setHTTPBody:jsonData];
  260. //
  261. // NSData *finalDataToDisplay = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
  262. // NSMutableDictionary *abc = [NSJSONSerialization JSONObjectWithData: finalDataToDisplay
  263. // options: NSJSONReadingMutableContainers
  264. // error: &error];
  265. // NSLog(@"%@", abc);
  266. //}
  267. - (void)selfReplayFunction{
  268. [[IDLFaceDetectionManager sharedInstance] reset];
  269. }
  270. @end