BDFaceDetectionViewController.m 13 KB

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