ApplyForShootController.m
46.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
//
// ApplyForShootController.m
// ApplyForDaren
//
// Created by cnliveJunBo on 2018/1/27.
// Copyright © 2018年 cnliveJunBo. All rights reserved.
//
#import "ApplyForShootController.h"
#import <AipOcrSdk/AipOcrSdk.h>
#import "AFNetworking.h"
#import "ApplyForShootCell.h"
#import "ApplyForConfirmCell.h"
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import <CNLiveRequestBastKit/CNLiveNetworkSessionManager.h>
#import "UIColor+CNLiveExtension.h"
#import "MJExtension.h"
#import "CNLiveBaseKit.h"
#import <QMUIKit/QMUIKit.h>
#import "CNLiveTimeTools.h"
#import "CNLiveBusinessTools.h"
#import "CNUserInfoManager.h"
#import "CNLiveBusinessTools.h"
#import "CNLiveEnvironment.h"
#import "CNLiveIDCardAuthGetImage.h"
#import <CNLiveImagePickerController/TZImagePickerController.h>
#import <CNLiveImagePickerController/TZImageManager.h>
#import <CNLiveCommonCategory/CNLiveCommonCategory.h>
#import "CNLiveIDCardAuthAlertView.h"
#import "CNLiveBPersonAuthentProtocol.h"
@interface ApplyForShootController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate, UIImagePickerControllerDelegate, IDCardAuthAlertViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray <AgreementModel*>*dataArray;
@property (nonatomic, strong) UIImage *fontImage;
@property (nonatomic, strong) UIImage *backImage;
@property (nonatomic, strong) UIImage *iconImage;
@property (nonatomic, strong) NSMutableArray *imageDatas;
@property (nonatomic, copy) NSString *realName;
@property (nonatomic, copy) NSString *idCardNo;
@property (nonatomic, copy) NSString *address;
@property (nonatomic, assign) BOOL isFont;
@property (nonatomic, assign) BOOL isDataSucc;
@property (nonatomic, assign) BOOL isFontSucc;
@property (nonatomic, assign) BOOL isBackSucc;
@property (nonatomic, assign) BOOL isOwnSucc;
@property (nonatomic, assign) BOOL isHandler;//是否监听识别结果
@property (nonatomic, strong) UIImagePickerController *pickerVC;
@property (nonatomic, strong) ApplyForConfirmCell *confirmCell;
@end
@implementation ApplyForShootController {
// 默认的识别成功的回调
void (^_successHandler)(id);
// 默认的识别失败的回调
void (^_failHandler)(NSError *);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"拍摄照片";
self.view.backgroundColor = [UIColor whiteColor];
self.edgesForExtendedLayout = UIRectEdgeNone;
// self.extendedLayoutIncludesOpaqueBars = YES;
self.imageDatas = [NSMutableArray array];
self.pickerVC = [[UIImagePickerController alloc] init];
self.pickerVC.delegate = self;
[[AipOcrService shardService] authWithAK:BDAppKey andSK:BDSecret];
[self loadData];
[self.view addSubview:self.tableView];
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}else{
self.automaticallyAdjustsScrollViewInsets = NO;
}
[self configCallback];
}
#pragma mark -百度识别回调!!!!!!!!!!!!!!!
- (void)configCallback {
__weak typeof(self) weakSelf = self;
// 这是默认的识别成功的回调
_successHandler = ^(id result){
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf dismissViewControllerAnimated:YES completion:nil];
});
if (!weakSelf.isHandler) {
return ;
}
weakSelf.isHandler = NO;
NSString *title = @"识别结果";
NSMutableString *message = [NSMutableString string];
if(result[@"words_result"]){
if([result[@"words_result"] isKindOfClass:[NSDictionary class]]){ ///// 字典
NSDictionary *dict = result[@"words_result"];
if (weakSelf.isFont) { ///////// 前
if ([dict count] < 6) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
CNLiveIDCardAuthAlertView *alertView = [[CNLiveIDCardAuthAlertView alloc] initWithTitle:title message:@"照片不规范,信息不全,请重新拍摄" delegate:weakSelf];
[alertView show];
weakSelf.isFontSucc = NO;
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [CNLiveIDCardAuthGetImage cnLiveIDCardAuth_getImageName:@"observer_idcard_front" bundle:@"CNBPersonCardInfo.bundle/CNBPersonCardInfo" targetClass:[ApplyForShootController class]];
cell.hideChangeLab = YES;
weakSelf.confirmCell.confirmButton.backgroundColor = RGB(216, 216, 216);
[weakSelf.confirmCell.confirmButton setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
}];
return ;
}
} else {
if ([dict count] < 3) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
CNLiveIDCardAuthAlertView *alertView = [[CNLiveIDCardAuthAlertView alloc] initWithTitle:title message:@"照片不规范,信息不全,请重新拍摄" delegate:weakSelf];
[alertView show];
weakSelf.isBackSucc = NO;
NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [CNLiveIDCardAuthGetImage cnLiveIDCardAuth_getImageName:@"observer_idcard_back" bundle:@"CNBPersonCardInfo.bundle/CNBPersonCardInfo" targetClass:[ApplyForShootController class]];
cell.hideChangeLab = YES;
weakSelf.confirmCell.confirmButton.backgroundColor = RGB(216, 216, 216);
[weakSelf.confirmCell.confirmButton setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
}];
return ;
} else if ([[CNLiveTimeTools getyyMMddStr] integerValue] > [dict[@"失效日期"][@"words"] integerValue] && ![[NSString stringWithFormat:@"%@",dict[@"失效日期"][@"words"]] isEqualToString:@"长期"]) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
CNLiveIDCardAuthAlertView *alertView = [[CNLiveIDCardAuthAlertView alloc] initWithTitle:title message:@"身份证已失效,请重新拍摄" delegate:weakSelf];
[alertView show];
weakSelf.isBackSucc = NO;
NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [CNLiveIDCardAuthGetImage cnLiveIDCardAuth_getImageName:@"observer_idcard_back" bundle:@"CNBPersonCardInfo.bundle/CNBPersonCardInfo" targetClass:[ApplyForShootController class]];
cell.hideChangeLab = YES;
weakSelf.confirmCell.confirmButton.backgroundColor = RGB(216, 216, 216);
[weakSelf.confirmCell.confirmButton setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
}];
return ;
}
}
//创建一个队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//可开辟线程的最大数量
queue.maxConcurrentOperationCount = 2;
weakSelf.isDataSucc = YES;
//创建两个任务
NSBlockOperation *operationA = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"A任务当前线程为:%@", [NSThread currentThread]);
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if([obj isKindOfClass:[NSDictionary class]] && [obj objectForKey:@"words"] && ![[obj objectForKey:@"words"] isEqualToString:@""]){
[message appendFormat:@"%@: %@\n", key, obj[@"words"]];
}else{
[message appendFormat:@"%@: %@\n", key, obj];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
CNLiveIDCardAuthAlertView *alertView = [[CNLiveIDCardAuthAlertView alloc] initWithTitle:title message:@"照片不规范,获取信息失败,请重新拍摄" delegate:weakSelf];
[alertView show];
weakSelf.isDataSucc = NO;
if (weakSelf.isFont) {
weakSelf.isFontSucc = NO;
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [CNLiveIDCardAuthGetImage cnLiveIDCardAuth_getImageName:@"observer_idcard_front" bundle:@"CNBPersonCardInfo.bundle/CNBPersonCardInfo" targetClass:[ApplyForShootController class]];
cell.hideChangeLab = YES;
weakSelf.confirmCell.confirmButton.backgroundColor = RGB(216, 216, 216);
[weakSelf.confirmCell.confirmButton setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
} else {
weakSelf.isBackSucc = NO;
NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [CNLiveIDCardAuthGetImage cnLiveIDCardAuth_getImageName:@"observer_idcard_back" bundle:@"CNBPersonCardInfo.bundle/CNBPersonCardInfo" targetClass:[ApplyForShootController class]];
cell.hideChangeLab = YES;
weakSelf.confirmCell.confirmButton.backgroundColor = RGB(216, 216, 216);
[weakSelf.confirmCell.confirmButton setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
}
}];
*stop = YES;
}
}];
}];
NSBlockOperation *operationB = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"B任务当前线程为:%@", [NSThread currentThread]);
dispatch_async(dispatch_get_main_queue(), ^{
if (!weakSelf.isDataSucc) {
return ;
}
if (weakSelf.isFont) {
weakSelf.realName = dict[@"姓名"][@"words"];
weakSelf.idCardNo = dict[@"公民身份号码"][@"words"];
weakSelf.address = dict[@"住址"][@"words"];
weakSelf.isFontSucc = YES;
NSString *message = [NSString stringWithFormat:@"姓名:%@\n身份证号码:%@\n住址:%@",weakSelf.realName,weakSelf.idCardNo,weakSelf.address];
CNLiveIDCardAuthAlertView *alertView = [[CNLiveIDCardAuthAlertView alloc] initWithTitle:@"请确认身份证信息是否准确" message:message delegate:weakSelf];
alertView.messageLabTextAlignmentLeft = YES;
[alertView show];
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = weakSelf.fontImage;
cell.hideChangeLab = NO;
if (weakSelf.isBackSucc && weakSelf.isOwnSucc) {
weakSelf.confirmCell.confirmButton.backgroundColor = RGBA(76, 170, 252, 1);
[weakSelf.confirmCell.confirmButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
}
} else {
weakSelf.isBackSucc = YES;
NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = weakSelf.backImage;
cell.hideChangeLab = NO;
if (weakSelf.isFontSucc && weakSelf.isOwnSucc) {
weakSelf.confirmCell.confirmButton.backgroundColor = RGBA(76, 170, 252, 1);
[weakSelf.confirmCell.confirmButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
}
}
[weakSelf dismissViewControllerAnimated:YES completion:nil];
});
}];
// operationB 任务依赖于 operationA
[operationB addDependency:operationA];
//添加操作到队列中(自动异步执行任务,并发)
[queue addOperation:operationA];
[queue addOperation:operationB];
}else if([result[@"words_result"] isKindOfClass:[NSArray class]]){ //////有可能是 数组
NSArray *array = result[@"words_result"];
if (weakSelf.isFont) {
if ([array count] < 6) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
CNLiveIDCardAuthAlertView *alertView = [[CNLiveIDCardAuthAlertView alloc] initWithTitle:title message:@"照片不规范,信息不全,请重新拍摄" delegate:weakSelf];
[alertView show];
weakSelf.isFontSucc = NO;
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [CNLiveIDCardAuthGetImage cnLiveIDCardAuth_getImageName:@"observer_idcard_front" bundle:@"CNBPersonCardInfo.bundle/CNBPersonCardInfo" targetClass:[ApplyForShootController class]];
cell.hideChangeLab = YES;
weakSelf.confirmCell.confirmButton.backgroundColor = RGB(216, 216, 216);
[weakSelf.confirmCell.confirmButton setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
}];
return ;
}
} else {
if ([array count] < 3) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
CNLiveIDCardAuthAlertView *alertView = [[CNLiveIDCardAuthAlertView alloc] initWithTitle:title message:@"照片不规范,信息不全,请重新拍摄" delegate:weakSelf];
[alertView show];
weakSelf.isBackSucc = NO;
NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [CNLiveIDCardAuthGetImage cnLiveIDCardAuth_getImageName:@"observer_idcard_back" bundle:@"CNBPersonCardInfo.bundle/CNBPersonCardInfo" targetClass:[ApplyForShootController class]];
cell.hideChangeLab = YES;
weakSelf.confirmCell.confirmButton.backgroundColor = RGB(216, 216, 216);
[weakSelf.confirmCell.confirmButton setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
}];
return ;
}
}
weakSelf.isDataSucc = YES;
for(NSDictionary *obj in result[@"words_result"]){
if([obj isKindOfClass:[NSDictionary class]] && [obj objectForKey:@"words"] && ![[obj objectForKey:@"words"] isEqualToString:@""]){
[message appendFormat:@"%@\n", obj[@"words"]];
}else{
[message appendFormat:@"%@\n", obj];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
CNLiveIDCardAuthAlertView *alertView = [[CNLiveIDCardAuthAlertView alloc] initWithTitle:title message:@"照片不规范,信息获取失败,请重新拍摄" delegate:weakSelf];
[alertView show];
weakSelf.isDataSucc = NO;
if (weakSelf.isFont) {
weakSelf.isFontSucc = NO;
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [CNLiveIDCardAuthGetImage cnLiveIDCardAuth_getImageName:@"observer_idcard_front" bundle:@"CNBPersonCardInfo.bundle/CNBPersonCardInfo" targetClass:[ApplyForShootController class]];
cell.hideChangeLab = YES;
weakSelf.confirmCell.confirmButton.backgroundColor = RGB(216, 216, 216);
[weakSelf.confirmCell.confirmButton setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
} else {
weakSelf.isBackSucc = NO;
NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [CNLiveIDCardAuthGetImage cnLiveIDCardAuth_getImageName:@"observer_idcard_back" bundle:@"CNBPersonCardInfo.bundle/CNBPersonCardInfo" targetClass:[ApplyForShootController class]];
cell.hideChangeLab = YES;
weakSelf.confirmCell.confirmButton.backgroundColor = RGB(216, 216, 216);
[weakSelf.confirmCell.confirmButton setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
}
}];
return ;
}
}
dispatch_async(dispatch_get_main_queue(), ^{
if (!weakSelf.isDataSucc) {
return ;
}
if (weakSelf.isFont) {
weakSelf.isFontSucc = YES;
for(NSDictionary *obj in array) {
[obj enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if ([key isEqualToString:@"姓名"]) {
weakSelf.realName = obj[@"姓名"][@"words"];
}else if ([key isEqualToString:@"公民身份号码"]){
weakSelf.idCardNo = obj[@"公民身份号码"][@"words"];
}else if ([key isEqualToString:@"住址"]){
weakSelf.address = obj[@"住址"][@"words"];
}
}];
}
NSString *message = [NSString stringWithFormat:@"姓名:%@\n身份证号码:%@\n住址:%@",weakSelf.realName,weakSelf.idCardNo,weakSelf.address];
CNLiveIDCardAuthAlertView *alertView = [[CNLiveIDCardAuthAlertView alloc] initWithTitle:@"请确认身份证信息是否准确" message:message delegate:weakSelf];
alertView.messageLabTextAlignmentLeft = YES;
[alertView show];
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = weakSelf.fontImage;
cell.hideChangeLab = NO;
if (weakSelf.isBackSucc && weakSelf.isOwnSucc) {
weakSelf.confirmCell.confirmButton.backgroundColor = RGBA(76, 170, 252, 1);
[weakSelf.confirmCell.confirmButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
}
} else {
weakSelf.isBackSucc = YES;
NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = weakSelf.backImage;
cell.hideChangeLab = NO;
if (weakSelf.isFontSucc && weakSelf.isOwnSucc) {
weakSelf.confirmCell.confirmButton.backgroundColor = RGBA(76, 170, 252, 1);
[weakSelf.confirmCell.confirmButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
}
}
[weakSelf dismissViewControllerAnimated:YES completion:nil];
});
}
}else{
[message appendFormat:@"%@", result];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
CNLiveIDCardAuthAlertView *alertView = [[CNLiveIDCardAuthAlertView alloc] initWithTitle:title message:@"照片识别失败,请重试" delegate:weakSelf];
[alertView show];
if (weakSelf.isFont) {
weakSelf.isFontSucc = NO;
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [CNLiveIDCardAuthGetImage cnLiveIDCardAuth_getImageName:@"observer_idcard_front" bundle:@"CNBPersonCardInfo.bundle/CNBPersonCardInfo" targetClass:[ApplyForShootController class]];
cell.hideChangeLab = YES;
weakSelf.confirmCell.confirmButton.backgroundColor = RGB(216, 216, 216);
[weakSelf.confirmCell.confirmButton setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
} else {
weakSelf.isBackSucc = NO;
NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [CNLiveIDCardAuthGetImage cnLiveIDCardAuth_getImageName:@"observer_idcard_back" bundle:@"CNBPersonCardInfo.bundle/CNBPersonCardInfo" targetClass:[ApplyForShootController class]];
cell.hideChangeLab = YES;
weakSelf.confirmCell.confirmButton.backgroundColor = RGB(216, 216, 216);
[weakSelf.confirmCell.confirmButton setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
}
}];
}
};
_failHandler = ^(NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf dismissViewControllerAnimated:YES completion:nil];
});
if (!weakSelf.isHandler) {
return ;
}
weakSelf.isHandler = NO;
NSString *msg = [NSString stringWithFormat:@"%li:%@", (long)[error code], [error localizedDescription]];
NSLog(@">>>>>>>>>>>>>>>>>>>>>>error:%@==msg:%@", error,msg);
NSString *errorMsg;
if ((long)[error code]==283504) {
errorMsg = @"网络请求失败,请稍后再试";
} else {
errorMsg = @"识别失败,请重拍";
}
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
CNLiveIDCardAuthAlertView *alertView = [[CNLiveIDCardAuthAlertView alloc] initWithTitle:@"识别结果" message:[NSString stringWithFormat:@"%@",errorMsg] delegate:weakSelf];
[alertView show];
if (weakSelf.isFont) {
weakSelf.isFontSucc = NO;
NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [CNLiveIDCardAuthGetImage cnLiveIDCardAuth_getImageName:@"observer_idcard_front" bundle:@"CNBPersonCardInfo.bundle/CNBPersonCardInfo" targetClass:[ApplyForShootController class]];
cell.hideChangeLab = YES;
weakSelf.confirmCell.confirmButton.backgroundColor = RGB(216, 216, 216);
[weakSelf.confirmCell.confirmButton setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
} else {
weakSelf.isBackSucc = NO;
NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [CNLiveIDCardAuthGetImage cnLiveIDCardAuth_getImageName:@"observer_idcard_back" bundle:@"CNBPersonCardInfo.bundle/CNBPersonCardInfo" targetClass:[ApplyForShootController class]];
cell.hideChangeLab = YES;
weakSelf.confirmCell.confirmButton.backgroundColor = RGB(216, 216, 216);
[weakSelf.confirmCell.confirmButton setTitleColor:RGB(180, 180, 180) forState:UIControlStateNormal];
}
}];
};
}
#pragma mark -
#pragma mark - 提交 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- (void)submit:(UIButton *)sender {
sender.selected = !sender.selected;
sender.userInteractionEnabled = NO;
if (!self.isFontSucc) {
sender.selected = !sender.selected;
sender.userInteractionEnabled = YES;
[QMUITips showWithText:@"请上传证件照片(前)" inView:self.view hideAfterDelay:1.5];
return;
} else if (!self.isBackSucc) {
sender.selected = !sender.selected;
sender.userInteractionEnabled = YES;
[QMUITips showWithText:@"请上传证件照片(后)" inView:self.view hideAfterDelay:1.5];
return;
} else if (!self.isOwnSucc) {
sender.selected = !sender.selected;
sender.userInteractionEnabled = YES;
[QMUITips showWithText:@"请上传本人照片" inView:self.view hideAfterDelay:1.5];
return;
}
NSMutableDictionary *params = [@{
@"sid" : CNUserShareModel.uid?CNUserShareModel.uid:@"",
@"realName" : self.realName?self.realName:@"",
@"idCardNo" : self.idCardNo?self.idCardNo:@"",
@"address" : self.address?self.address:@"",
} mutableCopy];
NSMutableDictionary *para = [CNLiveBusinessTools encryptionSignWithParameter:[NSMutableDictionary dictionaryWithDictionary:params] encryptionKey:APPKey];
__weak typeof(self) weakSelf = self;
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[QMUITips showLoadingInView:[UIApplication sharedApplication].delegate.window];
NSString *url = cn_API_Host(@"/Daren/buser/saveIdentity.action");
[manager POST:url parameters:para headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSData *fontData = [weakSelf imageDatasWithImage:self.fontImage];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss:SSS";
NSString *fileName = [NSString stringWithFormat:@"%@%@.png",[formatter stringFromDate:[NSDate date]],@(1)];
if (fontData) {
[formData appendPartWithFileData:fontData name:[NSString stringWithFormat:@"idCardObv"] fileName:fileName mimeType:@"image/png"]; // mimeType: image/jpg,image/png
}
NSData *backData = [weakSelf imageDatasWithImage:self.backImage];
NSString *fileName2 = [NSString stringWithFormat:@"%@%@.png",[formatter stringFromDate:[NSDate date]],@(2)];
if (backData) {
[formData appendPartWithFileData:backData name:[NSString stringWithFormat:@"idCardRev"] fileName:fileName2 mimeType:@"image/png"]; // mimeType: image/jpg,image/png
}
NSData *iconData = [weakSelf imageDatasWithImage:self.iconImage];
NSString *fileName3 = [NSString stringWithFormat:@"%@%@.png",[formatter stringFromDate:[NSDate date]],@(3)];
if (iconData) {
[formData appendPartWithFileData:iconData name:[NSString stringWithFormat:@"icon"] fileName:fileName3 mimeType:@"image/png"];
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[QMUITips hideAllTipsInView:[UIApplication sharedApplication].delegate.window];
if (!responseObject) {
sender.selected = !sender.selected;
sender.userInteractionEnabled = YES;
[QMUITips showError:@"接口错误,请稍后重试!" inView:self.view hideAfterDelay:1.5];
return ;
}
if ([[NSString stringWithFormat:@"%@",responseObject[@"errorCode"]] isEqualToString:@"0"]) {
//成功
// CNBPersonalVerificationController *vc = [[CNBPersonalVerificationController alloc]init];
// [self.navigationController pushViewController:vc animated:NO];
id <CNLiveBPersonAuthentProtocol>service = [[BeeHive shareInstance]createService:@protocol(CNLiveBPersonAuthentProtocol)];
UIViewController *vc = [service pushToAddPerAuthInstitutionsController:@{}];
[self.navigationController pushViewController:vc animated:YES];
} else {//接口失败
sender.selected = !sender.selected;
sender.userInteractionEnabled = YES;
[QMUITips showError:responseObject[@"errorMessage"] inView:self.view hideAfterDelay:1.5];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[QMUITips hideAllTipsInView:self.view];
sender.selected = !sender.selected;
sender.userInteractionEnabled = YES;
[QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:1.5];
}];
}
// Image 转 Data
- (NSData *)imageDatasWithImage:(UIImage *)image {
NSData *imageData;
if (UIImagePNGRepresentation(image) != nil) {
imageData = UIImagePNGRepresentation(image);
}else{
imageData = UIImageJPEGRepresentation(image, 1.0);
}
return imageData;
}
#pragma mark - 调用百度SDK识别 !!!!!!!!!!!!!!!!
- (void)goToBaiDuOrcController:(BOOL)isFront {
self.isHandler = YES;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
{
//无权限
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *appName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
if (appName == nil)
{
appName = @"APP";
}
// NSString *errorStr = @"无相机权限,请在设置中启用";
NSString *message = [NSString stringWithFormat:@"请在iPhone的“设置-隐私”选项中,允许%@访问你的相机。", appName];
UIAlertController *actionAlert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
// UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// // 引导去设置
// NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
// if ([[UIApplication sharedApplication]canOpenURL:url]) {
// [[UIApplication sharedApplication]openURL:url];
// }
// }];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:NULL];
[actionAlert addAction:action2];
// [actionAlert addAction:action1];
[self presentViewController:actionAlert animated:YES completion:nil];
// [QMUITips showError:errorStr inView:self.view hideAfterDelay:1.5f];
return;
}
if (isFront) {
_isFont = YES;
UIViewController * vc =
[AipCaptureCardVC ViewControllerWithCardType:CardTypeIdCardFont
andImageHandler:^(UIImage *image) {
self.fontImage = image;
[[AipOcrService shardService] detectIdCardFrontFromImage:image
withOptions:nil
successHandler:_successHandler
failHandler:_failHandler];
}];
[self presentViewController:vc animated:YES completion:nil];
} else if (!isFront) {
_isFont = NO;
UIViewController * vc =
[AipCaptureCardVC ViewControllerWithCardType:CardTypeIdCardBack
andImageHandler:^(UIImage *image) {
self.backImage = image;
[[AipOcrService shardService] detectIdCardBackFromImage:image
withOptions:nil
successHandler:_successHandler
failHandler:_failHandler];
}];
[self presentViewController:vc animated:YES completion:nil];
}
}
// 自拍cell点击事件
- (void)clickedIconImageAction {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if ((authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) && iOS7Later) {
// 无权限 做一个友好的提示
[self goPrivacySetting];
} else if (authStatus == AVAuthorizationStatusNotDetermined) {
// fix issue 466, 防止用户首次拍照拒绝授权时相机页黑屏
if (iOS7Later) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
dispatch_sync(dispatch_get_main_queue(), ^{
[self showBottomSelectView];
});
}
}];
} else {
[self showBottomSelectView];
}
} else {
[self showBottomSelectView];
}
}
#pragma mark - 显示下方选择拍照/相册视图
-(void)showBottomSelectView {
__weak __typeof(self)weakSelf = self;
[self.view createAlertViewTitleArray:@[@"拍照",@"从手机相册选择"] subTitleArr:@[@"",@""] textColor:RGBA(76, 170, 252, 1) subTitleColor:[UIColor whiteColor] firstFont:UIFontMake(18.0f) font:UIFontMake(18.0f) subFont:UIFontMake(18.0f) cancelTitle:@"取消" cancelFont:UIFontMake(18.0f) cancelTitleColor:UIColorMake(102, 102, 102) actionBlock:^(UIButton *button, NSInteger didRow) {
switch (didRow) {
case 0:{
[weakSelf pushXTPhoto];
}
break;
case 1:{
[weakSelf pushTZImagePickerController];
}
break;
default:
break;
}
}];
}
#pragma mark - 跳转到系统相机
-(void)pushXTPhoto {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.pickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
//设置拍照后的图片可被编辑
self.pickerVC.modalPresentationStyle = UIModalPresentationFullScreen;
self.pickerVC.cameraDevice = UIImagePickerControllerCameraDeviceFront;
self.pickerVC.allowsEditing = YES;
[self presentViewController:self.pickerVC animated:YES completion:nil];
}
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
NSString *key = nil;
if (picker.allowsEditing) {
key = UIImagePickerControllerEditedImage;
}
else {
key = UIImagePickerControllerOriginalImage;
}
//获取图片
UIImage *image = [info objectForKey:key];
//设置头像
[self handleIconWithImage:image];
//关闭相册界面
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
}
- (void)handleIconWithImage:(UIImage *)image {
// 固定方向
image = [image fixOrientation];//这个方法是UIImage+Extras.h中方法
//压缩图片质量
image = [self reduceImage:image percent:0.5];
// CGSize imageSize = image.size;
// imageSize.height = 320;
// imageSize.width = 320;
// image = [self imageWithImageSimple:image scaledToSize:imageSize];
__weak typeof(self)weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
NSIndexPath *index = [NSIndexPath indexPathForRow:2 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = image;
cell.hideChangeLab = NO;
weakSelf.isOwnSucc = YES;
weakSelf.iconImage = image;
if (weakSelf.isFontSucc && weakSelf.isBackSucc) {
weakSelf.confirmCell.confirmButton.backgroundColor = RGBA(76, 170, 252, 1);
[weakSelf.confirmCell.confirmButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
}
});
}
//压缩图片质量
-(UIImage *)reduceImage:(UIImage *)image percent:(float)percent {
NSData *imageData = UIImageJPEGRepresentation(image, percent);
UIImage *newImage = [UIImage imageWithData:imageData];
return newImage;
}
//压缩图片尺寸
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
//当用户取消选择的时候,调用该方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:^{}];
}
// 隐私设置提醒
- (void)goPrivacySetting {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];
if (!appName) appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleName"];
NSString *message = [NSString stringWithFormat:@"请去-> [设置 - 隐私 - 相机 - %@] 打开访问开关",appName];
QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"确定" style:QMUIAlertActionStyleCancel handler:^(__kindof QMUIAlertController *aAlertController, QMUIAlertAction *action) {
}];
QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"警告" message:message preferredStyle:QMUIAlertControllerStyleAlert];
[alertController addAction:action1];
[alertController showWithAnimated:YES];
});
}
#pragma mark - 从相册选择
- (void)pushTZImagePickerController {
__weak typeof(self)weakSelf = self;
TZImagePickerController *picker = [[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:self];
picker.modalPresentationStyle = UIModalPresentationFullScreen;
picker.allowPickingVideo = NO;
picker.allowPreview = NO;
picker.allowPickingGif = NO;
picker.allowTakePicture = NO;
[picker setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
dispatch_async(dispatch_get_main_queue(), ^{
NSIndexPath *index = [NSIndexPath indexPathForRow:2 inSection:0];
ApplyForShootCell *cell = [weakSelf.tableView cellForRowAtIndexPath:index];
cell.imgV.image = [photos firstObject];
cell.hideChangeLab = NO;
weakSelf.isOwnSucc = YES;
weakSelf.iconImage = [photos firstObject];
if (weakSelf.isFontSucc && weakSelf.isBackSucc) {
weakSelf.confirmCell.confirmButton.backgroundColor = RGBA(76, 170, 252, 1);
[weakSelf.confirmCell.confirmButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
}
});
}];
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark - IDCardAuthAlertViewDelegate
- (void)IDCardAlertView:(CNLiveIDCardAuthAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[alertView removeFromSuperview];
alertView = nil;
}
#pragma mark - 加载页面数据 !!!!!!!!!!!!!!!!!
- (void)loadData {
NSArray *array = @[
@{
@"shootTitle":@"第一步",
@"shootImage":@"observer_idcard_front",
@"isFront":@"1",
@"isOwn":@"0"
},
@{
@"shootTitle":@"第二步",
@"shootImage":@"observer_idcard_back",
@"isFront":@"0",
@"isOwn":@"0"
},
@{
@"shootTitle":@"第三步",
@"shootImage":@"observer_idcard_own",
@"isFront":@"0",
@"isOwn":@"1"
},
];
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dic in array) {
AgreementModel *model = [AgreementModel mj_objectWithKeyValues:dic];
[arrayM addObject:model];
}
self.dataArray = arrayM.copy;
[self.tableView reloadData];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArray.count+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row < self.dataArray.count) {
AgreementModel *model = self.dataArray[indexPath.row];
NSString *shootCellId = [NSString stringWithFormat:@"%zd",indexPath.row];
ApplyForShootCell *cell = [tableView dequeueReusableCellWithIdentifier:shootCellId];
if (!cell) {
cell = [[ApplyForShootCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:shootCellId];
cell.model = model;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
__weak __typeof(self)weakSelf = self;
cell.clickShootBtnBlock = ^() {
if (model.isOwn) {
//自拍cell点击事件
[weakSelf clickedIconImageAction];
} else {
//身份证
[weakSelf goToBaiDuOrcController:model.isFront];
}
};
return cell;
} else {
NSString *confirmCellId = [NSString stringWithFormat:@"%zd",indexPath.row];
ApplyForConfirmCell *cell = [tableView dequeueReusableCellWithIdentifier:confirmCellId];
if (!cell) {
cell = [[ApplyForConfirmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:confirmCellId];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
AgreementModel *model = [[AgreementModel alloc] init];
model.confirmTitle = @"下一步";
cell.model = model;
__weak __typeof(self)weakSelf = self;
cell.clickBtnBlock = ^(UIButton *btn){
[weakSelf submit:btn];
};
self.confirmCell = cell;
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row < self.dataArray.count) {
if (indexPath.row == 2) {
return 299;
} else {
return 256;
}
} else {
if (kNavigationBarHeight+256*2+145<KScreenHeight) {
return KScreenHeight-kNavigationBarHeight-256*2;
} else {
return 145;
}
}
}
#pragma mark - 返回 !!!!!!!!!!!!!!!!!!!
- (void)backAction
{
[self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)shouldPopViewControllerByBackButtonOrPopGesture:(BOOL)byPopGesture {
[self backAction];
return NO;
}
#pragma mark - SubViews
-(UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-kNavigationBarHeight) style:UITableViewStylePlain];
_tableView.backgroundColor = RGB(242, 242, 242);
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = NO;
_tableView.showsVerticalScrollIndicator = NO;
_tableView.showsHorizontalScrollIndicator = NO;
}
return _tableView;
}
- (void)dealloc {
NSLog(@"dealloc ==> ApplyForShootController");
}
@end