Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // CRNCardRecognitionViewController.m
- // CardRecognition
- //
- // Created by Andrey Karpetz on 9/8/14.
- // Copyright (c) 2014 Azoft. All rights reserved.
- //
- #import "CRNCardRecognitionViewController.h"
- #import "CRNUserCardViewController.h"
- #import "WOCardRecognizer.h"
- #import "UIImage+CRN.h"
- #import "CRNRecognizedCard.h"
- #import "CRNUserCardViewController.h"
- #import "UINavigationBar+Addition.h"
- #import "CRNNewUserCardViewController.h"
- #import "PCDConfiguration.h"
- #import "CRNSoundPlayer.h"
- #import "CRNRecognizedCardUploader.h"
- #import "PCDPayment.h"
- #import <AVFoundation/AVFoundation.h>
- #import "NAQCardRecognitionViewController.h"
- #import "UIDevice-Hardware.h"
- @interface CRNCardRecognitionViewController ()
- @property (nonatomic) WOCardRecognizer *recognizer;
- @property (weak, nonatomic) IBOutlet UIView *recognizerView;
- @property (weak, nonatomic) IBOutlet UIButton *addCardButton;
- @property (nonatomic) UIColor *previNavBarTintColor;
- @property (nonatomic) CGFloat previNavBarAlpha;
- @property (weak, nonatomic) IBOutlet UILabel *infoLabel;
- @property (weak, nonatomic) IBOutlet UIImageView *logoImageView;
- @property (weak, nonatomic) IBOutlet UIButton *flashButton;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *recognizerViewWidthConstraint;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *recognizerViewHeightConstraint;
- @property (nonatomic) BOOL recognitionIsActive;
- @end
- @implementation CRNCardRecognitionViewController
- + (NSString *)nibName {
- return NSStringFromClass([CRNCardRecognitionViewController class]);
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(handleAppBecomeActiveNotification)
- name:UIApplicationDidBecomeActiveNotification
- object:nil];
- }
- - (void)localizeUI {
- [self.addCardButton setTitle:LOC(@"recognition.addCardButton.title") forState:UIControlStateNormal];
- }
- - (NSString *)navigationBarTitle {
- return isPad ? LOC(@"recognition.navigationBar.title") : nil;
- }
- - (void)handleAppBecomeActiveNotification {
- if (self.recognitionIsActive) {
- [self.recognizer resumeRecognition];
- }
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- self.flashButton.hidden = ![self hasTorch];
- [self customizeNavBar];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- if (!_recognizer) {
- [self initializeRecognition];
- }
- else {
- [_recognizer resumeRecognition];
- }
- self.recognitionIsActive = YES;
- // [self.recognizer turnCameraFlash];
- [self setRightButtonItem:[self rightButtonItem]];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- [self restoreNavBar];
- [self.recognizer turnOffFlash];
- [self.recognizer pauseRecognition];
- self.recognitionIsActive = NO;
- }
- - (void)customizeNavBar {
- if (!isPad) {
- [self.navigationController.navigationBar crnHideCustomColorLayer:YES];
- self.previNavBarTintColor = self.navigationController.navigationBar.barTintColor;
- self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:100.0/255.0
- green:100.0/255.0
- blue:100.0/255.0
- alpha:1];
- self.previNavBarAlpha = [self.navigationController.navigationBar crnAlpha];
- [self.navigationController.navigationBar crnChangeAlpha:0.5];
- }
- }
- - (void)restoreNavBar {
- if (!isPad) {
- [self.navigationController.navigationBar crnHideCustomColorLayer:NO];
- self.navigationController.navigationBar.barTintColor = self.previNavBarTintColor;
- [self.navigationController.navigationBar crnChangeAlpha:self.previNavBarAlpha];
- }
- }
- - (UIBarButtonItem *)rightButtonItem {
- if (isPad) {
- return nil;
- }
- UIButton *splashButton = [[UIButton alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 28.0f, 28.0f)];
- UIImage *splashImage = [UIImage crnImageNamed:@"FlashButton.png"];
- [splashButton setImage:splashImage forState:UIControlStateNormal];
- [splashButton addTarget:self action:@selector(onFlashButtonTap) forControlEvents:UIControlEventTouchUpInside];
- UIBarButtonItem *splashButtonItem = [[UIBarButtonItem alloc] initWithCustomView:splashButton];
- return splashButtonItem;
- }
- - (void)initializeRecognition {
- AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
- if (status == AVAuthorizationStatusAuthorized) {
- [self makeRecognizer];
- }
- else if (status == AVAuthorizationStatusNotDetermined) {
- [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (granted) {
- [self makeRecognizer];
- } else {
- [self handleNoCameraAccess];
- }
- });
- }];
- }
- else {
- [self handleNoCameraAccess];
- }
- }
- - (void)makeRecognizer {
- self.infoLabel.text = LOC(@"recognition.infoLabel");
- BOOL isIphone4S = [[UIDevice currentDevice] crnPlatformType] == UIDevice4SiPhone;
- BOOL isIpod = [[UIDevice currentDevice] crnDeviceFamily] == UIDeviceFamilyiPod;
- NSString *numberNTTFilePath = [[NSBundle crnDefaultBundle] pathForResource:@"cnn_pan_24x32_m71_mce0.0435_als" ofType:@"nna"];
- NSString *dateNTTFilePath = [[NSBundle crnDefaultBundle] pathForResource:@"cnn_date_32x44_m78_mce0.0239685" ofType:@"nna"];
- NSString *holderNameNTTFilePath = (isIphone4S || isIpod) ? nil :[[NSBundle crnDefaultBundle] pathForResource:@"cnn_holder_32x44_alvls_506140_m304_mce0.0848" ofType:@"nna"];
- NSString *holderLocalizerFilePath = [[NSBundle crnDefaultBundle] pathForResource:@"cascade_lbp0.6" ofType:@"xml"];
- NSString *holderLocalizerNNTFilePath = [[NSBundle crnDefaultBundle] pathForResource:@"cnn_loc_20x27" ofType:@"nna"];
- self.recognizer = [[WOCardRecognizer alloc] initWithDelegate:self
- allowChangeOrientation:UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad];
- [self.recognizer setViewFrame:self.recognizerView.frame];
- [self updateRecognizerViewFrame];
- [self.recognizer setCardFramePortraitImage:[UIImage crnImageNamed:@"PortraitFrame.png"]
- landscapeImage:[UIImage crnImageNamed:@"LandscapeFrame.png"]];
- [self.recognizer loadViolaJonesCascades:[[NSBundle crnDefaultBundle] pathForResource:@"viola_jones_78x20_ALS_NCS10_NSF2_FAR0.5_TPR0.999_N4" ofType:@"xml"]];
- [self.recognizer loadHolderLocilizer:holderLocalizerFilePath cnnFileName:holderLocalizerNNTFilePath];
- [self.recognizer loadNeuralNetworks:numberNTTFilePath nnNameForDate:dateNTTFilePath nnNameForHolder:holderNameNTTFilePath];
- [self.recognizerView addSubview:_recognizer.view];
- [_recognizer view].frame = self.recognizerView.bounds;
- [self.recognizerView bringSubviewToFront:self.infoLabel];
- [self.recognizerView bringSubviewToFront:self.flashButton];
- [self.recognizerView bringSubviewToFront:self.logoImageView];
- }
- - (void)handleNoCameraAccess {
- self.infoLabel.text = LOC(@"recognition.infoLabel.noCameraAccess");
- [self.view layoutIfNeeded];
- }
- - (BOOL)hasTorch {
- NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
- BOOL hasTorch = NO;
- for (AVCaptureDevice *device in devices) {
- if (device.hasTorch && device.isTorchAvailable) {
- hasTorch = YES;
- break;
- }
- }
- return hasTorch;
- }
- #pragma mark - Helpers
- - (void)fakeRecognizeCard {
- // NSMutableArray *digitImages = [NSMutableArray new];
- // for (int i = 0; i < 16; i++) {
- // UIImage *image = [UIImage crnImageNamed:[NSString stringWithFormat:@"%d_digit", i]];
- // [digitImages addObject:image];
- // }
- // NSMutableArray *dateImages = [NSMutableArray new];
- // for (int i = 0; i < 4; i++) {
- // UIImage *image = [UIImage crnImageNamed:[NSString stringWithFormat:@"%d_digit", i]];
- // [dateImages addObject:image];
- // }
- NSDictionary *info = @{
- WOCardNumber : @"6764502570017028",
- WOPanRect : [NSValue valueWithCGRect:CGRectMake(46, 184, 415, 32)],
- WOExpDate : @"0216",
- WOHolderName : @"A DREY KARPETS",
- WOCardImage : [UIImage crnImageNamed:@"testCard"],
- WOHolderNameConfidences: @[@1,@0,@1,@1,@1,@1,@1,@1,@1,@1,@1,@1,@1,@1]
- };
- [self cardDidRecognize:info];
- }
- - (void)updateRecognizerViewFrame {
- if (isPad) {
- const CGFloat ipadDefaultWidth = 540;
- const CGFloat ipadDefaultHeigth = 590;
- BOOL isPortraint = UIInterfaceOrientationIsPortrait(self.interfaceOrientation);
- self.recognizerViewWidthConstraint.constant = isPortraint ? ipadDefaultWidth : ipadDefaultHeigth;
- self.recognizerViewHeightConstraint.constant = isPortraint ? ipadDefaultHeigth : ipadDefaultWidth;
- [UIView animateWithDuration:.2 animations:^{
- [self.view layoutIfNeeded];
- }];
- }
- else {
- [self.recognizer setViewFrame:self.recognizerView.frame];
- }
- }
- #pragma mark - WOCardRecognizerDelegate
- - (void)cardDidRecognize:(NSDictionary*)cardInfo {
- [self.recognizer pauseRecognition];
- [CRNSoundPlayer playSoundIfAllowed:@"captureCard"];
- NSMutableDictionary *extendedCardInfo = [cardInfo mutableCopy];
- extendedCardInfo[WORecognizerVersion] = [self.recognizer getReleaseVersion];
- CRNRecognizedCard *card = [[CRNRecognizedCard alloc] initWithCardRecognizerInfo:extendedCardInfo];
- [self uploadRecognizedCard:card];
- [self onCardRecognize:card];
- }
- #pragma mark - Actions
- - (IBAction)onManualCardAddButonTap:(id)sender {
- [self onAddCardManual];
- }
- - (IBAction)onFlashButtonTap {
- [self.recognizer turnCameraFlash];
- //[self fakeRecognizeCard];
- }
- //
- - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
- if (isPad) {
- [self updateRecognizerViewFrame];
- }
- }
- #pragma mark - Protected
- - (void)onCardRecognize:(CRNRecognizedCard *)card {
- }
- - (void)uploadRecognizedCard:(CRNRecognizedCard *)card {
- }
- - (void)onAddCardManual {
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement