自定义ZbarView



//  ZbarScanViewController.h

//  ShanghaiKepu

//

//  Created by 王云 on 16/3/28.

//  Copyright © 2016 王云. All rights reserved.

//


#import <UIKit/UIKit.h>

#import "Header.h"

#import "ZBarSDK.h"


@interface ZbarScanViewController : UIViewController


@end


//

//  ZbarScanViewController.m

//  ShanghaiKepu

//

//  Created by 王云 on 16/3/28.

//  Copyright © 2016 王云. All rights reserved.

//


#import "ZbarScanViewController.h"

#import "ZBarReaderView.h"

#import "SetupViewController.h"

#import "AFURLSessionManager.h"

#import "AFNetworking.h"

#import "MBProgressHUD.h"


@interface ZbarScanViewController ()<ZBarReaderViewDelegate,UIImagePickerControllerDelegate,MBProgressHUDDelegate>

{

    NSUserDefaults *ud;

    NSTimer *timeSaoyisao;

    int num;

    int count;

    BOOL upOrdown;

    NSTimer * timer;

    ZBarReaderView *readerViewZbar;

}

//二维码上层现实框

@property(nonatomic,strong) UIView *topView;

//开关灯

@property(nonatomic,strong) UIButton *lightButton;

//扫码线

@property (nonatomic, strong) UIImageView * line;

//正确提示

@property(nonatomic,strong) UIView *correctView;

//错误提示

@property(nonatomic,strong) UIView *errorView;

//电子票编号

@property(nonatomic,strong) NSString *ticketNum;

@end


@implementation ZbarScanViewController


-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(animation1) userInfo:nil repeats:YES];

     count = 0;

    [readerViewZbar start];

}


- (void)viewDidLoad {

    [super viewDidLoad];

    [self.navigationController setNavigationBarHidden:NO];

    self.navigationItem.title = @"上海科普票务验证";

    ud = [NSUserDefaults standardUserDefaults];

    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil];

    [self.navigationController.navigationBar setTitleTextAttributes:attributes];

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:245/255.0 green:118/255.0 blue:147/255.0 alpha:1];

    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];

    self.navigationItem.leftBarButtonItem = leftBarItem;


    [self scan];

}


//二维码上层现实框

-(UIView *)topView{

    if (!_topView) {

        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kSCreen_width, 50)];

        _topView.backgroundColor = [UIColor colorWithRed:245/255.0 green:118/255.0 blue:147/255.0 alpha:0.4];

        

        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 120, 50)];

        nameLabel.text = [ud objectForKey:@"nickname"];

        nameLabel.textAlignment = NSTextAlignmentLeft;

        nameLabel.textColor = [UIColor colorWithRed:245/255.0 green:118/255.0 blue:147/255.0 alpha:1];

        [_topView addSubview:nameLabel];

        

        

        UIButton *setBT = [UIButton buttonWithType:UIButtonTypeCustom];

        setBT.frame = CGRectMake(kSCreen_width-60, 0, 50, 50);

        [setBT setImage:[UIImage imageNamed:@"btn_setting"] forState:UIControlStateNormal];

        [setBT addTarget:self action:@selector(pushSetting) forControlEvents:UIControlEventTouchUpInside];

        [_topView addSubview:setBT];

    }

    return _topView;

}


//跳转到设置界面

-(void)pushSetting{

    SetupViewController *setVC = [[SetupViewController alloc] init];

    [self.navigationController pushViewController:setVC animated:YES];

}


//开关灯

-(UIButton *)lightButton{

    if (!_lightButton) {

        _lightButton = [UIButton buttonWithType:UIButtonTypeCustom];

        _lightButton.frame = CGRectMake((kSCreen_width-60)/2, kSCreen_height-100, 60, 60);

        [_lightButton setBackgroundImage:[UIImage imageNamed:@"btn_led_off"] forState:UIControlStateNormal];

        _lightButton.tag = 100;

        [_lightButton addTarget:self action:@selector(lightOrClose:) forControlEvents:UIControlEventTouchUpInside];

    }

    return _lightButton;

}


//关灯

//-(UIButton *)closeButton{

//    if (!_closeButton) {

//        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];

//        _closeButton.frame = CGRectMake(kSCreen_width-100, kSCreen_height-100, 60, 60);

//        _closeButton.tag = 200;

//        [_closeButton setBackgroundImage:[UIImage imageNamed:@"btn_led_on"] forState:UIControlStateNormal];

//        [_closeButton addTarget:self action:@selector(lightOrClose:) forControlEvents:UIControlEventTouchUpInside];

//    }

//    return _closeButton;

//}


//开关灯方法

-(void)lightOrClose:(UIButton *)sender{

    count++;

    NSLog(@"count%d",count);

    if (count%2==1) {

        [_lightButton setBackgroundImage:[UIImage imageNamed:@"btn_led_on"] forState:UIControlStateSelected];

        readerViewZbar.torchMode = 1;

    }else{

        [_lightButton setBackgroundImage:[UIImage imageNamed:@"btn_led_off"] forState:UIControlStateSelected];

        readerViewZbar.torchMode = 0;

    }

}


//正确提示

-(UIView *)correctView{

    if (!_correctView) {

        _correctView = [[UIView alloc] initWithFrame:CGRectMake(0, kSCreen_height/2, kSCreen_width, kSCreen_height/2)];

        _correctView.backgroundColor = [UIColor whiteColor];

        UILabel *yanzhengLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kSCreen_width, kSCreen_height/10-10)];

        yanzhengLabel.backgroundColor = [UIColor colorWithRed:115/255.0 green:199/255.0 blue:69/255.0 alpha:1];

        yanzhengLabel.text = @"验证通过";

        yanzhengLabel.textColor = [UIColor whiteColor];

        yanzhengLabel.font = [UIFont systemFontOfSize:20];

        yanzhengLabel.textAlignment = NSTextAlignmentCenter;

        [_correctView addSubview:yanzhengLabel];

        

        UILabel *ticketLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kSCreen_height/10, kSCreen_width, 50)];

        ticketLabel.text = @"电子票编号";

        ticketLabel.textColor = [UIColor lightGrayColor];

        ticketLabel.textAlignment = NSTextAlignmentCenter;

        [_correctView addSubview:ticketLabel];

        

        UILabel *numLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kSCreen_height/10+40, kSCreen_width, kSCreen_height/10)];

        numLabel.text = [ud objectForKey:@"ticketmsg"];

        numLabel.font = [UIFont systemFontOfSize:22];

        numLabel.textAlignment = NSTextAlignmentCenter;

        [_correctView addSubview:numLabel];

        

        UIButton *correctButton = [UIButton buttonWithType:UIButtonTypeCustom];

         correctButton.frame = CGRectMake((kSCreen_width-80)/2, 3*kSCreen_height/10, 80, 80);

        [correctButton setBackgroundImage:[UIImage imageNamed:@"btn_result_pass"] forState:UIControlStateNormal];

        [correctButton addTarget:self action:@selector(correctBack) forControlEvents:UIControlEventTouchUpInside];

        [_correctView addSubview:correctButton];

        

    }

    return _correctView;

}


//点击继续扫描

-(void)correctBack{

    [self.correctView removeFromSuperview];

    [self scan];

}


//错误提示

-(UIView *)errorView{

    if (!_errorView) {

        _errorView = [[UIView alloc] initWithFrame:CGRectMake(0, kSCreen_height/2, kSCreen_width, kSCreen_height/2)];

        _errorView.backgroundColor = [UIColor whiteColor];

        UILabel *yanzhengLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kSCreen_width, kSCreen_height/10-10)];

        yanzhengLabel.backgroundColor = [UIColor colorWithRed:115/255.0 green:199/255.0 blue:69/255.0 alpha:1];

        yanzhengLabel.text = @"验证失败";

        yanzhengLabel.textColor = [UIColor whiteColor];

        yanzhengLabel.font = [UIFont systemFontOfSize:20];

        yanzhengLabel.textAlignment = NSTextAlignmentCenter;

        [_errorView addSubview:yanzhengLabel];

        

        UILabel *ticketLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kSCreen_height/10, kSCreen_width, 50)];

        ticketLabel.text = @"错误原因";

        ticketLabel.textColor = [UIColor lightGrayColor];

        ticketLabel.textAlignment = NSTextAlignmentCenter;

        [_errorView addSubview:ticketLabel];

        

        UILabel *reasonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kSCreen_height/10+40, kSCreen_width, kSCreen_height/10)];

        reasonLabel.text = [ud objectForKey:@"ticketmsg"];

        reasonLabel.font = [UIFont systemFontOfSize:22];

        reasonLabel.textAlignment = NSTextAlignmentCenter;

        [_errorView addSubview:reasonLabel];

        

        

        UIButton *errorButton = [UIButton buttonWithType:UIButtonTypeCustom];

        errorButton.frame = CGRectMake((kSCreen_width-80)/2, 3*kSCreen_height/10, 80, 80);

        [errorButton setBackgroundImage:[UIImage imageNamed:@"btn_result_error"] forState:UIControlStateNormal];

        [errorButton addTarget:self action:@selector(errorBack) forControlEvents:UIControlEventTouchUpInside];

        [_errorView addSubview:errorButton];

    }

    return _errorView;

}


//点击继续扫描

-(void)errorBack{

    [self.errorView removeFromSuperview];

    [self scan];

}



//扫描二维码

-(void)scan{

    readerViewZbar = [[ZBarReaderView alloc]init];

    readerViewZbar.frame = CGRectMake(0, 0, kSCreen_width,kSCreen_height);

    readerViewZbar.readerDelegate = self;

    //关闭闪光灯

    readerViewZbar.torchMode = 0;

    

    

    

    [readerViewZbar addSubview:self.topView];

    [readerViewZbar addSubview:self.lightButton];

    

    UILabel *lightLabel = [[UILabel alloc] initWithFrame:CGRectMake((kSCreen_width-60)/2, kSCreen_height-40, 60, 30)];

    lightLabel.text = @"开灯";

    lightLabel.textColor = [UIColor whiteColor];

    lightLabel.textAlignment = NSTextAlignmentCenter;

    [readerViewZbar addSubview:lightLabel];

  


    

     //扫描区域

    

    ZBarImageScanner * scanner = readerViewZbar.scanner;

    [scanner setSymbology:ZBAR_I25

                   config:ZBAR_CFG_ENABLE

                       to:0];

    

    UIImageView *scanImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"border_scan"]];

    scanImageView.frame = CGRectMake((kSCreen_width-kSCreen_height/3)/2, kSCreen_height/3, kSCreen_height/3, kSCreen_height/3);

    [readerViewZbar addSubview:scanImageView];

  

    _line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,kSCreen_height/3, 4)];

    _line.image = [UIImage imageNamed:@"line_scan"];

    [scanImageView addSubview:_line];

    

    //定时器,

//    timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(animation1) userInfo:nil repeats:YES];

   

    

    

    //处理模拟器

    if (TARGET_IPHONE_SIMULATOR) {

        ZBarCameraSimulator *cameraSimulator

        = [[ZBarCameraSimulator alloc]initWithViewController:self];

        cameraSimulator.readerView = readerViewZbar;

    }

    [self.view addSubview:readerViewZbar];

//    CGRect scanMaskRect = CGRectMake((kSCreen_width-kSCreen_height/3)/2, kSCreen_height/3, kSCreen_height/3, kSCreen_height/3);

    //扫描区域计算

 //   readerViewZbar.scanCrop = [self getScanCrop:scanMaskRect readerViewBounds:readerViewZbar.bounds];


    [readerViewZbar start];


}


//扫码线动画

-(void)animation1

{

   // NSLog(@"saoyisao");

    if (upOrdown == NO) {

        num ++;

        _line.frame = CGRectMake(0, 5*num, kSCreen_height/3, 4);

        if (num == (int)(kSCreen_height/3/5)) {

            upOrdown = YES;

        }

    }

    else {

        num --;

        _line.frame = CGRectMake(0, 5*num, kSCreen_height/3, 4);

        if (num == 0) {

            upOrdown = NO;

        }

    }

}


-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds

{

    CGFloat x,y,width,height;

    

    x = rect.origin.x / readerViewBounds.size.width;

    y = rect.origin.y / readerViewBounds.size.height;

    width = rect.size.width / readerViewBounds.size.width;

    height = rect.size.height / readerViewBounds.size.height;

    

    return CGRectMake(x, y, width, height);

}


#pragma mark-ZBarReaderViewDelegate

- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image

{

    for (ZBarSymbol *symbol in symbols) {

        NSLog(@"symbol%@", symbol.data);

        [ud setObject:symbol.data forKey:@"symbol"];

        break;

    }

    NSLog(@"haha");

    [readerView stop];

    [self saoyisao];

}



//扫码网络请求

-(void)saoyisao{

        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

        manager.requestSerializer     = [AFHTTPRequestSerializer serializer];

        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

        // manager.responseSerializer    = [AFHTTPResponseSerializer serializer];

    NSLog(@"token%@",[ud objectForKey:@"token"]);

        NSMutableData *data = [NSMutableData dataWithData:[NSJSONSerialization dataWithJSONObject:@{@"token":[ud objectForKey:@"token"],@"user_encode":[ud objectForKey:@"symbol"]} options:NSJSONWritingPrettyPrinted error:nil]];

        NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    

        [manager POST:@"http://kepu.xiaoheiban.cn/api-Manage-scanQrcode" parameters:@{@"key":jsonString} progress:^(NSProgress * _Nonnull uploadProgress) {

    

        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

            NSLog(@"JSON: %@", responseObject);

            if ([[responseObject objectForKey:@"code"] integerValue]==0) {

                NSLog(@"成功");

                [ud setObject:[responseObject objectForKey:@"msg"] forKey:@"ticketmsg"];

                [readerViewZbar addSubview:self.correctView];

            } else {

                NSLog(@"失败");

                [ud setObject:[responseObject objectForKey:@"msg"] forKey:@"ticketmsg"];

                [readerViewZbar addSubview:self.errorView];

            }

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

            NSLog(@"Error: %@", error);

            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];

            hud.delegate = self;

            hud.backgroundColor = [UIColor clearColor];

            hud.labelText = @"网络异常";

            hud.margin = 10.f;

            hud.yOffset = 100.f;

            hud.removeFromSuperViewOnHide = YES;

            [hud hide:YES afterDelay:1];

            [self scan];

        }];

}


-(void)viewDidDisappear:(BOOL)animated{

    [super viewDidDisappear:animated];

    _line.frame = CGRectMake(30, 10, 180, 4);

    //num = 0;

    upOrdown = NO;

    readerViewZbar.torchMode = 0;

    [readerViewZbar stop];

    count = 0;

    [timer invalidate];

    timer  = nil;

    [self.correctView removeFromSuperview];

    [self.errorView removeFromSuperview];

}


- (void)dealloc

{

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值