//
//  UIViewController+KankanOCR.m
//  Open
//
//  Created by 看看智能 on 2018/1/8.
//  Copyright © 2018年 黄江涛 . All rights reserved.
//

#import "UIViewController+KankanOCR.h"
#import "SCCaptureCameraController.h"
#import "IOSOCRAPI.h"
#import "Globaltypedef.h"


static const void *externHSx = &externHSx;
static const void *externHSx_1 = &externHSx_1;
static const void *externHSx_2 = &externHSx_2;
static const void *externHSx_3 = &externHSx_3;
static const void *externHSx_4 = &externHSx_4;
static const void *externHSx_5 = &externHSx_5;
static const void *externHSx_6 = &externHSx_6;
static const void *externHSx_7 = &externHSx_7;
static const void *externHSx_9 = &externHSx_9;
@implementation UIViewController (KankanOCR)


//ocrimage
-(NSString *)ocrImageStr{
    
    return objc_getAssociatedObject(self,externHSx );
}
-(void)setOcrImageStr:(NSString *)ocrImageStr
{
    objc_setAssociatedObject(self, externHSx, ocrImageStr, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSString *)ocrName{
    
    return objc_getAssociatedObject(self,externHSx_1 );
}
-(void)setOcrName:(NSString *)ocrName
{
      objc_setAssociatedObject(self, externHSx_1, ocrName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
}


-(NSString *)ocrSex{
    
    return objc_getAssociatedObject(self,externHSx_2 );
}
-(void)setOcrSex:(NSString *)ocrSex
{
    objc_setAssociatedObject(self, externHSx_2, ocrSex, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


-(NSString *)ocrID{
    
    return objc_getAssociatedObject(self,externHSx_3 );
}
-(void)setOcrID:(NSString *)ocrID
{
    objc_setAssociatedObject(self, externHSx_3, ocrID, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)ocrFolk
{
     return objc_getAssociatedObject(self,externHSx_9 );
    
}
-(void)setOcrFolk:(NSString *)ocrFolk
{
      objc_setAssociatedObject(self, externHSx_9, ocrFolk, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
}

-(NSString *)ocrBirthday{
    
    return objc_getAssociatedObject(self,externHSx_4 );
}
-(void)setOcrBirthday:(NSString *)ocrBirthday
{
    objc_setAssociatedObject(self, externHSx_4, ocrBirthday, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)ocrAddress{
    
    return objc_getAssociatedObject(self,externHSx_5 );
}
-(void)setOcrAddress:(NSString *)ocrAddress
{
    objc_setAssociatedObject(self, externHSx_5, ocrAddress, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark - BOOL类型的动态绑定
-( BOOL)isgetOCR{
    return [objc_getAssociatedObject(self, externHSx_7) boolValue];
}
-(void)setIsgetOCR:(BOOL)isgetOCR
{
    objc_setAssociatedObject(self, externHSx_7, [NSNumber numberWithBool:isgetOCR], OBJC_ASSOCIATION_ASSIGN);
}

-(void)startOCR:(BOOL)isgetORC{
    [self getOCR];
    SCCaptureCameraController *con = [[SCCaptureCameraController alloc] init];
    con.scNaigationDelegate = self;
    con.iCardType = TIDCARD2;
    con.isDisPlayTxt = NO;

    //con.TimeKey = TimeKeyS;
    con.ScanMode = TIDC_SCAN_MODE;
    [self presentViewController:con animated:YES completion:NULL];
    
}
-(void)getOCR
{
#if TARGET_IPHONE_SIMULATOR
    
#else
    // 打开引擎
    Gint ret = 0;
    Gint vaule = 1;
    ret = TREC_StartUP(TREC_GetEngineTimeKEY());
    if (ret == 100) {
        
        return;
    }
    if (ret == 100) {
        return;
    }if (ret == 101) {
        return;
    }
    
    ret = TREC_SetParam(T_SET_HEADIMG, &vaule);
    
    ret = TREC_SetSupportEngine(TIDCARD2);// 演示使用身份证，其他证件以此类推
    
    if (ret != 1) {
        TREC_ClearUP();
        return;
    }
    
    
    ret = TREC_ClearUP();
#endif
   
    
    
}

- (void)sendCardFaceImage:(UIImage *)image
{
    if (image != NULL) {
    }
}



- (void)sendIDCValue:(NSString *)name SEX:(NSString *)sex FOLK:(NSString *)folk BIRTHDAY:(NSString *)birthday ADDRESS:(NSString *) address NUM:(NSString *)num
{
    self.ocrName = name;
    self.ocrID =num;
    self.ocrSex = sex;
    self.ocrBirthday =birthday;
    self.ocrAddress =address;
    self.ocrFolk =folk;
    self.isgetOCR =YES;
}
- (void)sendTakeImage:(TCARD_TYPE) iCardType image:(UIImage *)cardImage
{
    NSData *small =UIImageJPEGRepresentation([self imageByScalingAndCroppingForSize:CGSizeMake(667, 375) and:cardImage], 1.0);
    self.ocrImageStr = [small base64EncodedStringWithOptions:0];
    

    
}
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize and:(UIImage*)imageKs
{
    UIImage *sourceImage = imageKs;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth= width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else if (widthFactor < heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    //UIGraphicsBeginImageContext(targetSize); // this will crop
    UIGraphicsImageRenderer*re=[[UIGraphicsImageRenderer alloc] initWithSize:targetSize];
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width= scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil)
        NSLog(@"could not scale image");
    
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;
}
- (IBAction)theThirdSenderButtTest:(UIButton *)sender {
    NSLog(@"123123");
}

@end
