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)setIDCardRecognizerDelegate:(nullable id<NHNCloudIDCardRecognizerDelegate>)delegate;
@protocol NHNCloudIDCardRecognizerDelegate <NSObject>
// 身分証認識結果を返す
- (void)didDetectIDCardInfo:(nullable NHNCloudIDCardInfo *)cardInfo error:(nullable NSError *)error;
@optional
// スクリーンキャプチャイベント受信
- (void)didDetectIDCardSecurityEvent:(NHNCloudSecurityEvent)event;
// 閉じるボタンイベント受信(NHNCloudIDCardRecognizerViewController継承実装時にのみ受信可能)
- (void)IDCardRecognizerViewControllerCancel;
// 確認ボタンイベント受信(NHNCloudIDCardRecognizerViewController継承実装時にのみ受信可能)
- (void)IDCardRecognizerViewControllerConfirm;
@end
@interface NHNCloudOCR : NSObject
//..
+ (void)setDetectedImageReturn:(BOOL)enable;
+ (BOOL)isEnableDetectedImageReturn;
//..
@end
@interface NHNCloudIDCardInfo: NSObject
// 身分証認識領域
@property(nonatomic, strong, readonly, nullable) NSArray<NSValue *> *numberBoundingBoxes;
@end
- (void)viewDidLoad {
[super viewDidLoad];
// 認識した画像を返すように設定
[NHNCloudOCR setDetectedImageReturn:YES];
}
// 身分証認識結果を返す
- (void)didDetectIDCardInfo:(NHNCloudIDCardInfo *)cardInfo error:(NSError *)error {
if (cardInfo.detectedImage != nil) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:cardInfo.detectedImage.image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
// 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
// デバイスの解像度を考慮してscaleの値で座標を分割します。
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 {
// 初期化およびDelegate設定
NHNCloudOCRConfiguration *configuration = [NHNCloudOCRConfiguration configurationWithAppKey:@"{AppKey}" secret:@"{Secret}"];
// 検出画像を返す設定
[NHNCloudOCR setDetectedImageReturn:YES];
// 初期化
[NHNCloudOCR initWithConfiguration:configuration];
// Delegate設定
[NHNCloudOCR setIDCardRecognizerDelegate:self];
}
// 身分証認識結果を返す
- (void)didDetectIDCardInfo:(NHNCloudIDCardInfo *)cardInfo error:(NSError *)error {
NSLog(@"didDetectIDCardInfo : cardInfo : %@", cardInfo);
NSLog(@"didDetectIDCardInfo : error : %@", error);
}
// スクリーンキャプチャイベント受信
- (void)didDetectIDCardSecurityEvent:(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;
}
}
}
// 確認ボタンイベント受信(NHNCloudIDCardRecognizerViewController継承実装時にのみ受信可能)
- (void)IDCardRecognizerViewControllerConfirm {
// 身分証認識結果画面で確認ボタンを押した時の処理
}
// 閉じるボタンイベント受信(NHNCloudIDCardRecognizerViewController継承実装時にのみ受信可能)
- (void)IDCardRecognizerViewControllerCancel {
// 身分証認識または結果画面で閉じるボタンを押した時の処理
}
@end
* NHNCloudIDCardRecognizerViewControllerを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)didUpdateIDCardGuide:(CGRect)rect;
// 身分証認識時、UI更新
- (void)imageDidDetect:(BOOL)detected;
@interface OCRViewController : NHNCloudIDCardRecognizerServiceViewController <NHNCloudIDCardRecognizerDelegate>
@end
@implementation OCRViewController
- (void)viewDidLoad {
[super viewDidLoad];
[NHNCloudOCR setIDCardRecognizerDelegate: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)didUpdateIDCardGuide:(CGRect)rect {
[super didUpdateIDCardGuide:rect];
// Custom UI更新
}
- (void)imageDidDetect:(BOOL)detected {
[super imageDidDetect:detected];
// 身分証認識時、UI更新
}
- (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 {
// 初期化およびDelegate設定
NHNCloudOCRConfiguration *configuration = [NHNCloudOCRConfiguration configurationWithAppKey:@"{AppKey}" secret:@"{Secret}" ];
[configuration enableTestGuide];
[NHNCloudOCR initWithConfiguration:configuration];
[NHNCloudOCR setIDCardRecognizerDelegate:self];
}
ID Card適用方法
を見てNHNCloudIDCardRecognizerViewControllerまたはNHNCloudIDCardRecognizerServiceViewController継承実装必要
- (void)startRunning;
- (void)stopRunning;
- (BOOL)isRunning;
- (void)start {
[self startRunning];
}
// 身分証認識結果を返す
- (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 // didDetectIDCardInfoの結果として受け取ったcardInfo
completionHandler:^(BOOL isAuthenticity, NSError * _Nullable error) {
if (isAuthenticity) {
// 身分証認識成功
} else {
// 身分証認識失敗
}
}];