NHN Cloud OCR operates in iOS 11.0 or higher.
The configuration of NHN Cloud OCR SDK for iOS is as follows.
Service | Cocoapods Pod Name | Framework | Dependency | Build Settings |
---|---|---|---|---|
OCR | NHNCloudOCR | NHNCloudOCR.framework | * Vision.framework * AVFoundation.framework |
|
Mandatory | NHNCloudCore NHNCloudCommon |
NHNCloudCore.framework NHNCloudCommon.framework |
OTHER_LDFLAGS = ( "-ObjC", "-lc++" ); |
platform :ios, '11.0'
use_frameworks!
target '{YOUR PROJECT TARGET NAME}' do
pod 'NHNCloudOCR'
end
Key : NSCameraUsageDescription
Value : [Camera Permission Request Message]
// reset
+ (void)initWithConfiguration:(NHNCloudOCRConfiguration *)configuration;
// set delegate
+ (void)setCreditCardRecognizerDelegate:(nullable id<NHNCloudCreditCardRecognizerDelegate>)delegate;
@protocol NHNCloudCreditCardRecognizerDelegate <NSObject>
// return credit card recognition result
- (void)didDetectCreditCardInfo:(nullable NHNCloudCreditCardInfo *)cardInfo error:(nullable NSError *)error;
@optional
// Receive screen capture events
- (void)didDetectCreditCardSecurityEvent:(NHNCloudSecurityEvent)event;
// Receive the close button event (can only be received when implementing NHNCloudCreditCardRecognizerViewController inheritance)
- (void)creditCardRecognizerViewControllerCancel;
// Receive confirmation button event (received only when inheriting NHNCloudCreditCardRecognizerViewController)
- (void)creditCardRecognizerViewControllerConfirm;
@end
@interface NHNCloudOCR : NSObject
//..
+ (void)setDetectedImageReturn:(BOOL)enable;
+ (BOOL)isEnableDetectedImageReturn;
//..
@end
@interface NHNCloudCreditCardInfo : NSObject
// Card number recognition area
@property(nonatomic, strong, readonly, nullable) NSArray<NSValue *> *numberBoundingBoxes;
// Expiration period recognition area
@property(nonatomic, assign, readonly) CGRect validThruBoundingBox;
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Set up to return recognized image
[NHNCloudOCR setDetectedImageReturn:YES];
}
// Return credit card recognition result
- (void)didDetectCreditCardInfo:(nullable NHNCloudCreditCardInfo *)cardInfo error:(nullable NSError *)error {
if (cardInfo.detectedImage != nil) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:cardInfo.detectedImage.image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIGraphicsBeginImageContextWithOptions(imageView.frame.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
// Draw the recognized area for credit card number.
for (NSValue *rectValue in cardInfo.numberBoundingBoxes) {
CGRect scaledBoundingBox = [self dividedRect:rectValue.CGRectValue
// Divide the coordinates by the value of scale based on the device's resolution.
scale:[UIScreen mainScreen].scale];
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
CGContextSetLineWidth(context, 5.0);
CGContextStrokeRect(context, scaledBoundingBox);
}
CGRect scaledValidThruBoundingBox = [self dividedRect:cardInfo.validThruBoundingBox
scale:[UIScreen mainScreen].scale];
// Draw the recognized area for expiration period
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
CGContextSetLineWidth(context, 5.0);
CGContextStrokeRect(context, scaledValidThruBoundingBox);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView.image = newImage;
[self.view addSubview:imageView];
}
}
- (CGRect)dividedRect:(CGRect)rect
scale:(CGFloat)scale {
return CGRectMake(rect.origin.x / scale, rect.origin.y / scale,
rect.size.width / scale, rect.size.height / scale);
}
#import <NHNCloudOCR/NHNCloudOCR.h>
@interface ViewController() <NHNCloudCreditCardRecognizerDelegate>
@end
@implementationViewController
- (void)initializeOCR {
// Initialization and Delegate settings
NHNCloudOCRConfiguration *configuration = [NHNCloudOCRConfiguration configurationWithAppKey:@"{AppKey}" secret:@"{Secret}"];
// set detection image return
[NHNCloudOCR setDetectedImageReturn:YES];
// reset
[NHNCloudOCR initWithConfiguration:configuration];
// set delegate
[NHNCloudOCR setCreditCardRecognizerDelegate:self];
}
// return credit card recognition result
- (void)didDetectCreditCardInfo:(nullable NHNCloudCreditCardInfo *)cardInfo error:(nullable NSError *)error {
NSLog(@"didDetectCreditCardInfo : cardInfo : %@", cardInfo);
NSLog(@"didDetectCreditCardInfo : error : %@", error);
}
// receive screen capture events
- (void)didDetectCreditCardSecurityEvent:(NHNCloudSecurityEvent)event {
// Example of screen capture alert output
if (event == NHNCloudSecurityEventScreenshot || event == NHNCloudSecurityEventScreenRecordingOn) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"Capture detected." preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
// Example of outputting a blank screen when recording a video
if (event == NHNCloudSecurityEventScreenRecordingOn || event == NHNCloudSecurityEventScreenRecordingOff) {
if ([[UIScreen mainScreen] isCaptured] ) {
[[[UIApplication sharedApplication] windows] firstObject].hidden = YES;
} else {
[[[UIApplication sharedApplication] windows] firstObject]. hidden = NO;
}
}
}
// Receive confirmation button event (can be received only when implementing NHNCloudCreditCardRecognizerViewController inheritance)
- (void)creditCardRecognizerViewControllerConfirm {
// Processing when the OK button is pressed on the credit card recognition result screen
}
// Receive the close button event (can only be received when implementing NHNCloudCreditCardRecognizerViewController inheritance)
- (void)creditCardRecognizerViewControllerCancel {
// Process when credit card recognition or close button is pressed on result screen
}
@end
* Create a ViewController Class with NHNCloudCreditCardRecognizerViewController as a subclass.
* Add ViewController to Storyboard.
* Set the created class in Custom Class in the added ViewController.
* Set the ViewController Segue Event.
// Perform some initial setup and data prep work when the view is created in memory
- (void)viewDidLoad;
// Perform the last thing right before the view comes on screen
- (void)viewWillAppear:(BOOL)animated;
// Perform some cleanup right before the view disappears from the screen
- (void)viewWillDisappear:(BOOL)animated;
// Perform additional cleanup after the view is completely off the screen
- (void)viewDidDisappear:(BOOL)animated;
// Update Custom UI
- (void)didUpdateCreditCardGuide:(CGRect)rect orientation:(NHNCloudCreditCardOrientation)orientation;
// Update UI when recognizing credit card
- (void)imageDidDetect:(BOOL)detected;
@interface OCRViewController : NHNCloudCreditCardRecognizerServiceViewController <NHNCloudCreditCardRecognizerDelegate>
@end
@implementation OCRViewController
- (void)viewDidLoad {
[super viewDidLoad];
[NHNCloudOCR setCreditCardRecognizerDelegate:self];
// Create Custom UI
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self startRunning];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
- (void)didUpdateCreditCardGuide:(CGRect)rect orientation:(NHNCloudCreditCardOrientation)orientation {
[super didUpdateCreditCardGuide:rect orientation:orientation];
// Update Custom UI
}
- (void)imageDidDetect:(BOOL)detected {
[super imageDidDetect:detected];
// Update UI when recognizing credit card
}
- (void)didDetectCreditCardInfo:(nullable NHNCloudCreditCardInfo *)cardInfo error:(nullable NSError *)error {
NSLog(@"didDetectCreditCardInfo : cardInfo : %@", cardInfo);
NSLog(@"didDetectCreditCardInfo : error : %@", error);
}
enableTestGuide
. @interface NHNCloudOCRConfiguration : NSObject
- (void)enableTestGuide;
@end
- (void)initializeOCR {
// Initialization and Configure Delegate
NHNCloudOCRConfiguration *configuration = [NHNCloudOCRConfiguration configurationWithAppKey:@"{AppKey}" secret:@"{Secret}" ];
[configuration enableTestGuide];
[NHNCloudOCR initWithConfiguration:configuration];
[NHNCloudOCR setCreditCardRecognizerDelegate:self];
}
Inherit and implement NHNCloudCreditCardRecognizerViewController or NHNCloudCreditCardRecognizerServiceViewController by referring to
How to Apply Credit Card
- (void)startRunning;
- (void)stopRunning;
- (BOOL)isRunning;
- (void)start {
[self startRunning];
}
// return credit card recognition result
- (void)didDetectCreditCardInfo:(nullable NHNCloudCreditCardInfo *)cardInfo error:(nullable NSError *)error {
[self stopRunning];
}
@property (assign, nonatomic, readonly) CGRect creditCardGuide;
@property (assign, nonatomic, readonly) NHNCloudCreditCardOrientation creditCardGuideOrientation;
- (void)rotateCreditCardGuideOrientation;
typedef NS_ENUM(NSInteger, NHNCloudCreditCardOrientation) {
NHNCloudCreditCardOrientationPortrait = 0,
NHNCloudCreditCardOrientationLandscape = 1
};
- (void)rotateButtonAction:(UIButton *)button {
[self rotateCreditCardGuideOrientation];
NSLog(@"x: %f y: %f width: %f height: %f", self.creditCardGuide.origin.x,
self.creditCardGuide.origin.y,
self.creditCardGuide.size.width,
self.creditCardGuide.size.height);
NSLog(@"creditCardGuideOrientation: %ld", self.creditCardGuideOrientation);
}
- (void)enableTorchMode;
- (void)disableTorchMode;
- (BOOL)isEnableTorchMode;
- (void)torchButtonAction:(UIButton *)button {
if ([self isEnableTorchMode] == YES) {
[self disableTorchMode];
} else {
[self enableTorchMode];
}
}
- (void)startRunningCamera;
- (void)stopRunningCamera;
- (BOOL)isRunnginCamera;
- (void)cameraButtonAction:(UIButton *)button {
if ([self isRunnginCamera] == YES) {
[self stopRunningCamera];
} else {
[self startRunningCamera];
}
}