//
//  GetProgressController.m
//  Open
//
//  Created by 看看智能 on 2017/11/30.
//  Copyright © 2017年 黄江涛 . All rights reserved.
//

#import "GetProgressController.h"

@interface GetProgressController ()<UITextFieldDelegate>
@property(nonatomic,strong)NSString *ocrImage;
@property(nonatomic,strong)UIImage *AR_image;

@property (weak, nonatomic) IBOutlet UIButton *senderYEs;

@property (weak, nonatomic) IBOutlet UIButton *reget;

@end

@implementation GetProgressController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.nameText.delegate =self;
    self.idText.delegate =self;

    self.ocrImage =self.SearchimageOCR;
    self.nameText.text=self.nameOCR ;
    self.idText.text= self.IDOCR ;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    //点击键盘上的return收起键盘
    [textField resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
 
    [self.view endEditing:YES];
  
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"添加证件";

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark - 重新采集

-(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];

    
}


- (IBAction)rephoto:(id)sender {
        [self maoasfdsfg];
}
#pragma mark - 确定

- (IBAction)senderButton:(id)sender {
    

    
    
    
    
    if ([self.nameText.text isEqualToString:@""] || [self.idText.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"身份证或者姓名为空"];
        return;
    }
    
     [SVProgressHUD show];
    [SearchProgressNet getbindIdCards:nil andUserName:self.nameText.text andIdentityNumber:self.idText.text andOCR:self.ocrImage Handle:^(SearchProgress *model, NSError *error) {
        if (model.code ==0) {
            [SVProgressHUD dismiss];
            SVProgressHUD.minimumDismissTimeInterval=1.0;

            
            if (self.ing ==10) {
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else{
                
                            [self.navigationController popViewControllerAnimated:YES];
            }
            
        }else if (model.code == 2){
            [SVProgressHUD showInfoWithStatus:@"未查询到您的认证信息，暂时无法绑定身份证"];
            return;
        }  else
        {
            [SVProgressHUD dismiss];
            codeSelected *code = [codeSelected MustTakeReturn:model.code];
            [code returnCodeMethod];
        }
        
        

    }];
    
    
}
#pragma mark - 调用OCR

-(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
{
    self.nameText.text =name;
    self.idText.text =  num;
    
}
- (void)sendTakeImage:(TCARD_TYPE) iCardType image:(UIImage *)cardImage
{
    NSData *small =UIImageJPEGRepresentation([self imageByScalingAndCroppingForSize:CGSizeMake(667, 375) and:cardImage], 1.0);
    self.ocrImage = [small base64EncodedStringWithOptions:0];
    self.AR_image =[UIImage imageWithData:small];
    
}
- (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
