NHN Cloud OCR은 iOS 11.0 이상에서 동작합니다.
iOS용 NHN Cloud OCR SDK의 구성은 다음과 같습니다.
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 : [카메라 권한 요청 메시지]
// 초기화
+ (void)initWithConfiguration:(NHNCloudOCRConfiguration *)configuration;
// Delegate 설정
+ (void)setCreditCardRecognizerDelegate:(nullable id<NHNCloudCreditCardRecognizerDelegate>)delegate;
@protocol NHNCloudCreditCardRecognizerDelegate <NSObject>
// 신용카드 인식 결과 반환
- (void)didDetectCreditCardInfo:(nullable NHNCloudCreditCardInfo *)cardInfo error:(nullable NSError *)error;
@optional
// 스크린 캡처 이벤트 수신
- (void)didDetectCreditCardSecurityEvent:(NHNCloudSecurityEvent)event;
// 닫기 버튼 이벤트 수신(NHNCloudCreditCardRecognizerViewController 상속 구현 시에만 수신 가능)
- (void)creditCardRecognizerViewControllerCancel;
// 확인 버튼 이벤트 수신(NHNCloudCreditCardRecognizerViewController 상속 구현 시에만 수신 가능)
- (void)creditCardRecognizerViewControllerConfirm;
@end
@interface NHNCloudOCR : NSObject
//..
+ (void)setDetectedImageReturn:(BOOL)enable;
+ (BOOL)isEnableDetectedImageReturn;
//..
@end
@interface NHNCloudCreditCardInfo : NSObject
// 카드 번호 인식 영역
@property(nonatomic, strong, readonly, nullable) NSArray<NSValue *> *numberBoundingBoxes;
// 유효 기간 인식 영역
@property(nonatomic, assign, readonly) CGRect validThruBoundingBox;
@end
- (void)viewDidLoad {
[super viewDidLoad];
// 인식된 이미지를 반환하도록 설정
[NHNCloudOCR setDetectedImageReturn:YES];
}
// 신용카드 인식 결과 반환
- (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)];
// 카드 번호의 인식 영역을 그린다.
for (NSValue *rectValue in cardInfo.numberBoundingBoxes) {
CGRect scaledBoundingBox = [self dividedRect:rectValue.CGRectValue
// 디바이스의 해상도를 고려해 scale의 값만큼 좌표를 나눈다.
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];
// 유효 기간의 인식 영역을 그린다.
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
@implementation ViewController
- (void)initializeOCR {
// 초기화 및 Delegate 설정
NHNCloudOCRConfiguration *configuration = [NHNCloudOCRConfiguration configurationWithAppKey:@"{AppKey}" secret:@"{Secret}"];
// 검출 이미지 반환 설정
[NHNCloudOCR setDetectedImageReturn:YES];
// 초기화
[NHNCloudOCR initWithConfiguration:configuration];
// Delegate 설정
[NHNCloudOCR setCreditCardRecognizerDelegate:self];
}
// 신용카드 인식 결과 반환
- (void)didDetectCreditCardInfo:(nullable NHNCloudCreditCardInfo *)cardInfo error:(nullable NSError *)error {
NSLog(@"didDetectCreditCardInfo : cardInfo : %@", cardInfo);
NSLog(@"didDetectCreditCardInfo : error : %@", error);
}
// 스크린 캡처 이벤트 수신
- (void)didDetectCreditCardSecurityEvent:(NHNCloudSecurityEvent)event {
// 스크린 캡처 경고 Alert 출력 예시
if (event == NHNCloudSecurityEventScreenshot || event == NHNCloudSecurityEventScreenRecordingOn) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"캡처가 감지되었습니다." preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
// 동영상 녹화 시 빈 화면 출력 예시
if (event == NHNCloudSecurityEventScreenRecordingOn || event == NHNCloudSecurityEventScreenRecordingOff) {
if ([[UIScreen mainScreen] isCaptured] ) {
[[[UIApplication sharedApplication] windows] firstObject].hidden = YES;
} else {
[[[UIApplication sharedApplication] windows] firstObject].hidden = NO;
}
}
}
// 확인 버튼 이벤트 수신(NHNCloudCreditCardRecognizerViewController 상속 구현 시에만 수신 가능)
- (void)creditCardRecognizerViewControllerConfirm {
// 신용카드 인식 결과 화면에서 확인 버튼을 눌렀을 때의 처리
}
// 닫기 버튼 이벤트 수신(NHNCloudCreditCardRecognizerViewController 상속 구현 시에만 수신 가능)
- (void)creditCardRecognizerViewControllerCancel {
// 신용카드 인식 또는 결과 화면에서 닫기 버튼을 눌렀을 때의 처리
}
@end
* NHNCloudCreditCardRecognizerViewController를 subclass로 가지는 ViewController Class를 생성합니다.
* Storyboard에 ViewController를 추가합니다.
* 추가한 ViewController에 Custom Class에 생성한 Class를 설정합니다.
* ViewController Segue Event를 설정합니다.
// 뷰가 메모리에 만들어질 때 초기 설정 및 데이터 준비 작업을 수행
- (void)viewDidLoad;
// 뷰가 화면에 나타나기 직전에 마지막 작업을 수행
- (void)viewWillAppear:(BOOL)animated;
// 뷰가 화면에서 사라지기 직전에 정리 작업을 수행
- (void)viewWillDisappear:(BOOL)animated;
// 뷰가 화면에서 완전히 사라진 후 추가적인 정리 작업을 수행
- (void)viewDidDisappear:(BOOL)animated;
// Custom UI 갱신
- (void)didUpdateCreditCardGuide:(CGRect)rect orientation:(NHNCloudCreditCardOrientation)orientation;
// 신용카드 인식 시 UI 갱신
- (void)imageDidDetect:(BOOL)detected;
@interface OCRViewController : NHNCloudCreditCardRecognizerServiceViewController <NHNCloudCreditCardRecognizerDelegate>
@end
@implementation OCRViewController
- (void)viewDidLoad {
[super viewDidLoad];
[NHNCloudOCR setCreditCardRecognizerDelegate:self];
// 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];
// Custom UI 갱신
}
- (void)imageDidDetect:(BOOL)detected {
[super imageDidDetect:detected];
// 신용카드 인식 시 UI 갱신
}
- (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 {
// 초기화 및 Delegate 설정
NHNCloudOCRConfiguration *configuration = [NHNCloudOCRConfiguration configurationWithAppKey:@"{AppKey}" secret:@"{Secret}" ];
[configuration enableTestGuide];
[NHNCloudOCR initWithConfiguration:configuration];
[NHNCloudOCR setCreditCardRecognizerDelegate:self];
}
Credit Card 적용 방법
을 보고 NHNCloudCreditCardRecognizerViewController 또는 NHNCloudCreditCardRecognizerServiceViewController 상속 구현 필요
- (void)startRunning;
- (void)stopRunning;
- (BOOL)isRunning;
- (void)start {
[self startRunning];
}
// 신용카드 인식 결과 반환
- (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];
}
}