NHN Cloud OCRはiOS 11.0以上で動作します。
iOS用NHN Cloud OCR SDKの構成は次のとおりです。
Service | Cocoapods Pod Name | Framework | Dependency | Build Settings |
---|---|---|---|---|
IAP | 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];
}
}