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)setIDCardRecognizerDelegate:(nullable id<NHNCloudIDCardRecognizerDelegate>)delegate;
@protocol NHNCloudIDCardRecognizerDelegate <NSObject>
// return ID recognition result
- (void)didDetectIDCardInfo:(nullable NHNCloudIDCardInfo *)cardInfo error:(nullable NSError *)error;
@optional
// Receive screen capture events
- (void)didDetectIDCardSecurityEvent:(NHNCloudSecurityEvent)event;
// Receive the close button event (can be received only when inheriting NHNCloudIDCardRecognizerViewController)
- (void)IDCardRecognizerViewControllerCancel;
// Receive OK button event (can be received only when inheriting NHNCloudIDCardRecognizerViewController)
- (void)IDCardRecognizerViewControllerConfirm;
@end
@interface NHNCloudOCR : NSObject
//..
+ (void)setDetectedImageReturn:(BOOL)enable;
+ (BOOL)isEnableDetectedImageReturn;
//..
@end
@interface NHNCloudIDCardInfo: NSObject
// ID card recognition area
@property(nonatomic, strong, readonly, nullable) NSArray<NSValue *> *boundingBoxes;
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Set up to return recognized image
[NHNCloudOCR setDetectedImageReturn:YES];
}
// Return ID card recognition result
- (void)didDetectIDCardInfo:(NHNCloudIDCardInfo *)cardInfo error:(NSError *)error {
if (cardInfo.detectedImage != nil) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:cardInfo.detectedImage.image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
// Draw recognition area on imageView
[self drawBoundingBoxes:cardInfo.boundingBoxes over:imageView];
[self.view addSubview:imageView];
}
}
- (void)drawBoundingBoxes:(NSArray *)boundingBoxes
over:(UIImageView *)imageView {
UIGraphicsBeginImageContextWithOptions(imageView.frame.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
for (NSValue *rectValue in boundingBoxes) {
CGRect boundingBox = [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, boundingBox);
}
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView.image = newImage;
}
- (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 () <NHNCloudIDCardRecognizerDelegate>
@end
@implementation ViewController
- (void)initializeOCR {
// Initialize and Configure Delegate
NHNCloudOCRConfiguration *configuration = [NHNCloudOCRConfiguration configurationWithAppKey:@"{AppKey}" secret:@"{Secret}"];
// Set Detected Image Return
[NHNCloudOCR setDetectedImageReturn:YES];
// Initialize
[NHNCloudOCR initWithConfiguration:configuration];
// Configure Delegate
[NHNCloudOCR setIDCardRecognizerDelegate:self];
}
// Return ID Card Recognition Result
- (void)didDetectIDCardInfo:(NHNCloudIDCardInfo *)cardInfo error:(NSError *)error {
NSLog(@"didDetectIDCardInfo : cardInfo : %@", cardInfo);
NSLog(@"didDetectIDCardInfo : error : %@", error);
}
// Receive Screen Capture Event
- (void)didDetectIDCardSecurityEvent:(NHNCloudSecurityEvent)event {
// Example of Screen Capture Alert
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 blank screen output when recording video
if (event == NHNCloudSecurityEventScreenRecordingOn || event == NHNCloudSecurityEventScreenRecordingOff) {
if ([[UIScreen mainScreen] isCaptured] ) {
[[[UIApplication sharedApplication] windows] firstObject].hidden = YES;
} else {
[[[UIApplication sharedApplication] windows] firstObject].hidden = NO;
}
}
}
// Receive Confirm Button Event (Only available for NHNCloudIDCardRecognizerViewController inheritance implementations)
- (void)IDCardRecognizerViewControllerConfirm {
// When you click the confirm button on the ID card recognition result
}
// Receive Close Button Event (Only available for NHNCloudIDCardRecognizerViewController inheritance implementations)
- (void)IDCardRecognizerViewControllerCancel {
// When you click the confirm button on the ID card recognition or result screen
}
@end
* Create a ViewController Class with NHNCloudIDCardRecognizerViewController 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)didUpdateIDCardGuide:(CGRect)rect;
// Update UI when recognizing ID
- (void)imageDidDetect:(BOOL)detected;
@interface OCRViewController : NHNCloudIDCardRecognizerServiceViewController <NHNCloudIDCardRecognizerDelegate>
@end
@implementation OCRViewController
- (void)viewDidLoad {
[super viewDidLoad];
[NHNCloudOCR setIDCardRecognizerDelegate: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)didUpdateIDCardGuide:(CGRect)rect {
[super didUpdateIDCardGuide:rect];
// Update Custom UI
}
- (void)imageDidDetect:(BOOL)detected {
[super imageDidDetect:detected];
// Update UI when recognizing ID
}
- (void)didDetectIDCardInfo:(nullable NHNCloudIDCardInfo *)cardInfo error:(nullable NSError *)error {
NSLog(@"didDetectIDCardInfo : cardInfo : %@", cardInfo);
NSLog(@"didDetectIDCardInfo : 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 setIDCardRecognizerDelegate:self];
}
Inherit and implement NHNCloudIDCardRecognizerViewController or NHNCloudIDCardRecognizerServiceViewController by referring to
How to Apply ID Card
- (void)startRunning;
- (void)stopRunning;
- (BOOL)isRunning;
- (void)start {
[self startRunning];
}
// return ID recognition result
- (void)didDetectIDCardInfo:(nullable NHNCloudIDCardInfo *)cardInfo error:(nullable NSError *)error {
[self stopRunning];
}
- (void)startRunningCamera;
- (void)stopRunningCamera;
- (BOOL)isRunnginCamera;
- (void)cameraButtonAction:(UIButton *)button {
if ([self isRunnginCamera] == YES) {
[self stopRunningCamera];
} else {
[self startRunningCamera];
}
}
+ (void)verificateAuthenticityIDCard:(nonnull NHNCloudIDCardInfo *)IDCardInfo
completionHandler:(nullable void (^)(BOOL isAuthenticity, NSError * _Nullable error))completionHandler
[NHNCloudOCR verificateAuthenticityIDCard:cardInfo // cardInfo received as a result of didDetectIDCardInfo
completionHandler:^(BOOL isAuthenticity, NSError * _Nullable error) {
if (isAuthenticity) {
// ID recognition successful
} else {
// ID recognition failed
}
}];