//
//  ViewController.m
//  OCR
//
//  Created by huawei on 2018/10/31.
//  Copyright © 2018/10/31 huawei. All rights reserved.
//

#import "ViewController.h"
#import "HWOcrClientAKSK.h"
#import "HWOcrClientToken.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *uriLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UITextView *resultTextView;

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self tokenOcrService];
    
//    [self akskOcrService];
}
- (void)tokenOcrService{
# warning Set required parameters
    NSString *domainName = @"xxx";// if the user isn't IAM user, domain_name is the same with username
    NSString *userName = @"xxx";
    NSString *password = @"xxx";
    NSString *uri = @"/v1.0/ocr/id-card";
    UIImage *image = [UIImage imageNamed:@"id-card-demo"];
    
    self.uriLabel.text = uri;
    self.imageView.image = image;
    
    // initialize HWOcrClientToken
    HWOcrClientToken *token = [[HWOcrClientToken alloc] initWithDomainName:domainName userName:userName password:password region:@"cn-north-4" endPoint:@"ocr.cn-north-4.myhuaweicloud.com"];
    
    // request ocr service
    __weak typeof(self) weakSelf = self;
    [token requestOcrServiceWithUri:uri image:image options:nil
                     successHandler:^(id result) {
                         dispatch_async(dispatch_get_main_queue(), ^{
                             weakSelf.resultTextView.text = (NSString *)result;
                         });
                     } failHandler:^(NSError *errMsg, NSInteger errCode) {
                         dispatch_async(dispatch_get_main_queue(), ^{
                             weakSelf.resultTextView.text = @"识别报错";
                         });
                     }];
    
}
- (void)akskOcrService{
# warning Set required parameters
    NSString *AK = @"xxx";// AK from authentication
    NSString *SK = @"xxx";// SK from authentication
    
    NSString *httpEndPoint = @"ocr.cn-north-4.myhuaweicloud.com";// http endpoint information
    NSString *uri = @"/v1.0/ocr/id-card";
    UIImage *image = [UIImage imageNamed:@"id-card-demo"];
    
    self.uriLabel.text = uri;
    self.imageView.image = image;
    // initialize HWOcrClientAKSK from ak,sk and endpoint information
    HWOcrClientAKSK *aksk = [[HWOcrClientAKSK alloc] initWithAppKey:AK AppSecretKey:SK HttpEndPoint:httpEndPoint];
    
    __weak typeof(self) weakSelf = self;
    // request ocr service
    [aksk requestOcrServiceWithUri:uri image:image options:nil
                    successHandler:^(id result) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            weakSelf.resultTextView.text = (NSString *)result;
                        });
                    } failHandler:^(NSError *error, NSInteger errCode) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            weakSelf.resultTextView.text = @"识别报错";
                        });
                    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
