//
//  startProgressViewcontroller.m
//  Open
//
//  Created by 看看智能 on 2017/12/1.
//  Copyright © 2017年 黄江涛 . All rights reserved.
//

#import "startProgressViewcontroller.h"

@interface startProgressViewcontroller ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *dsfergerg;

@end

@implementation startProgressViewcontroller

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"社保认证状态";
    self.dsfergerg.constant =230 * [UIScreen mainScreen].bounds.size.width / 375;
    [ self getOCR];
}

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

    [self maoasfdsfg];
}



#pragma mark - 调用OCR
-(void)maoasfdsfg{
    
    //获取ocr
    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) {
        NSLog(@"引擎过期\n");
        return;
    }
    if (ret == 100) {
        NSLog(@"引擎过期\n");
        return;
    }if (ret == 101) {
        NSLog(@"引擎绑定失败\n");
        return;
    }
    //获取引擎信息，可以不用操作，可有可无
    //  self.nameText.text=  [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@\n%@\n",TREC_GetCopyrightInfoArray(),TREC_GetVersionArray(),TREC_GetTimeStatus()]];
    // 设置引擎参数
    ret = TREC_SetParam(T_SET_HEADIMG, &vaule);
    // 设置引擎类型
    ret = TREC_SetSupportEngine(TIDCARD2);// 演示使用身份证，其他证件以此类推
    if (ret != 1) {
        NSLog(@"引擎不支持\n");
        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
{
    
    
    
    GetProgressController *getVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"GetProgressController"];
    getVC.nameOCR =name;
    getVC.IDOCR =num;
    getVC.SearchimageOCR = self.imageOCR;
    getVC.ing =10;
AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    delegate.changeProgress =@"开始";
    [self.navigationController pushViewController:getVC animated:YES];
    
}
- (void)sendTakeImage:(TCARD_TYPE) iCardType image:(UIImage *)cardImage
{
    NSData *small =UIImageJPEGRepresentation([self imageByScalingAndCroppingForSize:CGSizeMake(667, 375) and:cardImage], 1.0);
    
    if([[small base64EncodedStringWithOptions:0] isEqualToString:@""]){
        [SVProgressHUD showErrorWithStatus:@"识别错误"];
    }else{
        self.imageOCR = [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;
}


@end
