Advertisement
Guest User

Untitled

a guest
Dec 4th, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.66 KB | None | 0 0
  1. //
  2. // CRNCardRecognitionViewController.m
  3. // CardRecognition
  4. //
  5. // Created by Andrey Karpetz on 9/8/14.
  6. // Copyright (c) 2014 Azoft. All rights reserved.
  7. //
  8.  
  9. #import "CRNCardRecognitionViewController.h"
  10.  
  11. #import "CRNUserCardViewController.h"
  12. #import "WOCardRecognizer.h"
  13. #import "UIImage+CRN.h"
  14. #import "CRNRecognizedCard.h"
  15. #import "CRNUserCardViewController.h"
  16. #import "UINavigationBar+Addition.h"
  17. #import "CRNNewUserCardViewController.h"
  18. #import "PCDConfiguration.h"
  19. #import "CRNSoundPlayer.h"
  20. #import "CRNRecognizedCardUploader.h"
  21. #import "PCDPayment.h"
  22. #import <AVFoundation/AVFoundation.h>
  23. #import "NAQCardRecognitionViewController.h"
  24. #import "UIDevice-Hardware.h"
  25.  
  26. @interface CRNCardRecognitionViewController ()
  27.  
  28. @property (nonatomic) WOCardRecognizer *recognizer;
  29. @property (weak, nonatomic) IBOutlet UIView *recognizerView;
  30. @property (weak, nonatomic) IBOutlet UIButton *addCardButton;
  31. @property (nonatomic) UIColor *previNavBarTintColor;
  32. @property (nonatomic) CGFloat previNavBarAlpha;
  33. @property (weak, nonatomic) IBOutlet UILabel *infoLabel;
  34. @property (weak, nonatomic) IBOutlet UIImageView *logoImageView;
  35. @property (weak, nonatomic) IBOutlet UIButton *flashButton;
  36. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *recognizerViewWidthConstraint;
  37. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *recognizerViewHeightConstraint;
  38. @property (nonatomic) BOOL recognitionIsActive;
  39.  
  40. @end
  41.  
  42. @implementation CRNCardRecognitionViewController
  43.  
  44. + (NSString *)nibName {
  45. return NSStringFromClass([CRNCardRecognitionViewController class]);
  46. }
  47.  
  48. - (void)dealloc {
  49. [[NSNotificationCenter defaultCenter] removeObserver:self];
  50. }
  51.  
  52. - (void)viewDidLoad {
  53. [super viewDidLoad];
  54.  
  55. [[NSNotificationCenter defaultCenter] addObserver:self
  56. selector:@selector(handleAppBecomeActiveNotification)
  57. name:UIApplicationDidBecomeActiveNotification
  58. object:nil];
  59.  
  60. }
  61.  
  62. - (void)localizeUI {
  63. [self.addCardButton setTitle:LOC(@"recognition.addCardButton.title") forState:UIControlStateNormal];
  64. }
  65.  
  66. - (NSString *)navigationBarTitle {
  67. return isPad ? LOC(@"recognition.navigationBar.title") : nil;
  68. }
  69.  
  70. - (void)handleAppBecomeActiveNotification {
  71. if (self.recognitionIsActive) {
  72. [self.recognizer resumeRecognition];
  73. }
  74. }
  75.  
  76. - (void)viewWillAppear:(BOOL)animated {
  77. [super viewWillAppear:animated];
  78.  
  79. self.flashButton.hidden = ![self hasTorch];
  80.  
  81. [self customizeNavBar];
  82. }
  83.  
  84. - (void)viewDidAppear:(BOOL)animated {
  85. [super viewDidAppear:animated];
  86.  
  87. if (!_recognizer) {
  88. [self initializeRecognition];
  89. }
  90. else {
  91. [_recognizer resumeRecognition];
  92. }
  93.  
  94. self.recognitionIsActive = YES;
  95. // [self.recognizer turnCameraFlash];
  96.  
  97. [self setRightButtonItem:[self rightButtonItem]];
  98.  
  99. }
  100.  
  101. - (void)viewWillDisappear:(BOOL)animated {
  102. [super viewWillDisappear:animated];
  103. [self restoreNavBar];
  104. [self.recognizer turnOffFlash];
  105. [self.recognizer pauseRecognition];
  106. self.recognitionIsActive = NO;
  107. }
  108.  
  109.  
  110. - (void)customizeNavBar {
  111. if (!isPad) {
  112. [self.navigationController.navigationBar crnHideCustomColorLayer:YES];
  113. self.previNavBarTintColor = self.navigationController.navigationBar.barTintColor;
  114. self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:100.0/255.0
  115. green:100.0/255.0
  116. blue:100.0/255.0
  117. alpha:1];
  118. self.previNavBarAlpha = [self.navigationController.navigationBar crnAlpha];
  119. [self.navigationController.navigationBar crnChangeAlpha:0.5];
  120. }
  121. }
  122.  
  123. - (void)restoreNavBar {
  124. if (!isPad) {
  125. [self.navigationController.navigationBar crnHideCustomColorLayer:NO];
  126. self.navigationController.navigationBar.barTintColor = self.previNavBarTintColor;
  127. [self.navigationController.navigationBar crnChangeAlpha:self.previNavBarAlpha];
  128. }
  129. }
  130.  
  131. - (UIBarButtonItem *)rightButtonItem {
  132. if (isPad) {
  133. return nil;
  134. }
  135. UIButton *splashButton = [[UIButton alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 28.0f, 28.0f)];
  136. UIImage *splashImage = [UIImage crnImageNamed:@"FlashButton.png"];
  137. [splashButton setImage:splashImage forState:UIControlStateNormal];
  138. [splashButton addTarget:self action:@selector(onFlashButtonTap) forControlEvents:UIControlEventTouchUpInside];
  139. UIBarButtonItem *splashButtonItem = [[UIBarButtonItem alloc] initWithCustomView:splashButton];
  140. return splashButtonItem;
  141. }
  142.  
  143. - (void)initializeRecognition {
  144. AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  145. if (status == AVAuthorizationStatusAuthorized) {
  146. [self makeRecognizer];
  147. }
  148. else if (status == AVAuthorizationStatusNotDetermined) {
  149. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  150. dispatch_async(dispatch_get_main_queue(), ^{
  151. if (granted) {
  152. [self makeRecognizer];
  153. } else {
  154. [self handleNoCameraAccess];
  155. }
  156. });
  157. }];
  158. }
  159. else {
  160. [self handleNoCameraAccess];
  161. }
  162. }
  163.  
  164. - (void)makeRecognizer {
  165. self.infoLabel.text = LOC(@"recognition.infoLabel");
  166.  
  167. BOOL isIphone4S = [[UIDevice currentDevice] crnPlatformType] == UIDevice4SiPhone;
  168. BOOL isIpod = [[UIDevice currentDevice] crnDeviceFamily] == UIDeviceFamilyiPod;
  169.  
  170. NSString *numberNTTFilePath = [[NSBundle crnDefaultBundle] pathForResource:@"cnn_pan_24x32_m71_mce0.0435_als" ofType:@"nna"];
  171. NSString *dateNTTFilePath = [[NSBundle crnDefaultBundle] pathForResource:@"cnn_date_32x44_m78_mce0.0239685" ofType:@"nna"];
  172. NSString *holderNameNTTFilePath = (isIphone4S || isIpod) ? nil :[[NSBundle crnDefaultBundle] pathForResource:@"cnn_holder_32x44_alvls_506140_m304_mce0.0848" ofType:@"nna"];
  173. NSString *holderLocalizerFilePath = [[NSBundle crnDefaultBundle] pathForResource:@"cascade_lbp0.6" ofType:@"xml"];
  174. NSString *holderLocalizerNNTFilePath = [[NSBundle crnDefaultBundle] pathForResource:@"cnn_loc_20x27" ofType:@"nna"];
  175.  
  176. self.recognizer = [[WOCardRecognizer alloc] initWithDelegate:self
  177. allowChangeOrientation:UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad];
  178. [self.recognizer setViewFrame:self.recognizerView.frame];
  179. [self updateRecognizerViewFrame];
  180. [self.recognizer setCardFramePortraitImage:[UIImage crnImageNamed:@"PortraitFrame.png"]
  181. landscapeImage:[UIImage crnImageNamed:@"LandscapeFrame.png"]];
  182.  
  183. [self.recognizer loadViolaJonesCascades:[[NSBundle crnDefaultBundle] pathForResource:@"viola_jones_78x20_ALS_NCS10_NSF2_FAR0.5_TPR0.999_N4" ofType:@"xml"]];
  184. [self.recognizer loadHolderLocilizer:holderLocalizerFilePath cnnFileName:holderLocalizerNNTFilePath];
  185.  
  186. [self.recognizer loadNeuralNetworks:numberNTTFilePath nnNameForDate:dateNTTFilePath nnNameForHolder:holderNameNTTFilePath];
  187.  
  188. [self.recognizerView addSubview:_recognizer.view];
  189. [_recognizer view].frame = self.recognizerView.bounds;
  190.  
  191. [self.recognizerView bringSubviewToFront:self.infoLabel];
  192. [self.recognizerView bringSubviewToFront:self.flashButton];
  193. [self.recognizerView bringSubviewToFront:self.logoImageView];
  194. }
  195.  
  196. - (void)handleNoCameraAccess {
  197. self.infoLabel.text = LOC(@"recognition.infoLabel.noCameraAccess");
  198. [self.view layoutIfNeeded];
  199. }
  200.  
  201. - (BOOL)hasTorch {
  202. NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  203. BOOL hasTorch = NO;
  204.  
  205. for (AVCaptureDevice *device in devices) {
  206. if (device.hasTorch && device.isTorchAvailable) {
  207. hasTorch = YES;
  208. break;
  209. }
  210. }
  211.  
  212. return hasTorch;
  213. }
  214.  
  215. #pragma mark - Helpers
  216.  
  217.  
  218. - (void)fakeRecognizeCard {
  219. // NSMutableArray *digitImages = [NSMutableArray new];
  220. // for (int i = 0; i < 16; i++) {
  221. // UIImage *image = [UIImage crnImageNamed:[NSString stringWithFormat:@"%d_digit", i]];
  222. // [digitImages addObject:image];
  223. // }
  224. // NSMutableArray *dateImages = [NSMutableArray new];
  225. // for (int i = 0; i < 4; i++) {
  226. // UIImage *image = [UIImage crnImageNamed:[NSString stringWithFormat:@"%d_digit", i]];
  227. // [dateImages addObject:image];
  228. // }
  229.  
  230. NSDictionary *info = @{
  231.  
  232. WOCardNumber : @"6764502570017028",
  233. WOPanRect : [NSValue valueWithCGRect:CGRectMake(46, 184, 415, 32)],
  234. WOExpDate : @"0216",
  235. WOHolderName : @"A DREY KARPETS",
  236. WOCardImage : [UIImage crnImageNamed:@"testCard"],
  237. WOHolderNameConfidences: @[@1,@0,@1,@1,@1,@1,@1,@1,@1,@1,@1,@1,@1,@1]
  238. };
  239.  
  240. [self cardDidRecognize:info];
  241. }
  242.  
  243. - (void)updateRecognizerViewFrame {
  244. if (isPad) {
  245. const CGFloat ipadDefaultWidth = 540;
  246. const CGFloat ipadDefaultHeigth = 590;
  247.  
  248. BOOL isPortraint = UIInterfaceOrientationIsPortrait(self.interfaceOrientation);
  249.  
  250. self.recognizerViewWidthConstraint.constant = isPortraint ? ipadDefaultWidth : ipadDefaultHeigth;
  251. self.recognizerViewHeightConstraint.constant = isPortraint ? ipadDefaultHeigth : ipadDefaultWidth;
  252. [UIView animateWithDuration:.2 animations:^{
  253. [self.view layoutIfNeeded];
  254. }];
  255. }
  256. else {
  257. [self.recognizer setViewFrame:self.recognizerView.frame];
  258. }
  259. }
  260.  
  261.  
  262. #pragma mark - WOCardRecognizerDelegate
  263.  
  264. - (void)cardDidRecognize:(NSDictionary*)cardInfo {
  265. [self.recognizer pauseRecognition];
  266.  
  267. [CRNSoundPlayer playSoundIfAllowed:@"captureCard"];
  268.  
  269. NSMutableDictionary *extendedCardInfo = [cardInfo mutableCopy];
  270. extendedCardInfo[WORecognizerVersion] = [self.recognizer getReleaseVersion];
  271.  
  272. CRNRecognizedCard *card = [[CRNRecognizedCard alloc] initWithCardRecognizerInfo:extendedCardInfo];
  273. [self uploadRecognizedCard:card];
  274. [self onCardRecognize:card];
  275. }
  276.  
  277. #pragma mark - Actions
  278.  
  279. - (IBAction)onManualCardAddButonTap:(id)sender {
  280. [self onAddCardManual];
  281. }
  282.  
  283. - (IBAction)onFlashButtonTap {
  284. [self.recognizer turnCameraFlash];
  285. //[self fakeRecognizeCard];
  286. }
  287. //
  288. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
  289. if (isPad) {
  290. [self updateRecognizerViewFrame];
  291. }
  292. }
  293.  
  294.  
  295. #pragma mark - Protected
  296.  
  297. - (void)onCardRecognize:(CRNRecognizedCard *)card {
  298. }
  299.  
  300. - (void)uploadRecognizedCard:(CRNRecognizedCard *)card {
  301. }
  302.  
  303. - (void)onAddCardManual {
  304.  
  305. }
  306.  
  307. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement