自定义ZbarView
来源:互联网 发布:苹果 app 办公软件 编辑:程序博客网 时间:2024/06/08 14:23
// 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 = [NSTimerscheduledTimerWithTimeInterval:0.2target:selfselector:@selector(animation1)userInfo:nilrepeats:YES];
count = 0;
[readerViewZbarstart];
}
- (void)viewDidLoad {
[superviewDidLoad];
[self.navigationControllersetNavigationBarHidden:NO];
self.navigationItem.title =@"上海科普票务验证";
ud = [NSUserDefaultsstandardUserDefaults];
NSDictionary *attributes=[NSDictionarydictionaryWithObjectsAndKeys:[UIColorwhiteColor],NSForegroundColorAttributeName,nil];
[self.navigationController.navigationBarsetTitleTextAttributes:attributes];
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:245/255.0green:118/255.0blue:147/255.0alpha:1];
UIBarButtonItem *leftBarItem = [[UIBarButtonItemalloc] initWithTitle:@""style:UIBarButtonItemStylePlaintarget:selfaction:nil];
self.navigationItem.leftBarButtonItem = leftBarItem;
[self scan];
}
//二维码上层现实框
-(UIView *)topView{
if (!_topView) {
_topView = [[UIViewalloc] initWithFrame:CGRectMake(0,64, kSCreen_width, 50)];
_topView.backgroundColor = [UIColorcolorWithRed:245/255.0green:118/255.0blue:147/255.0alpha:0.4];
UILabel *nameLabel = [[UILabelalloc] initWithFrame:CGRectMake(10,0, 120, 50)];
nameLabel.text = [udobjectForKey:@"nickname"];
nameLabel.textAlignment = NSTextAlignmentLeft;
nameLabel.textColor = [UIColorcolorWithRed:245/255.0green:118/255.0blue:147/255.0alpha:1];
[_topView addSubview:nameLabel];
UIButton *setBT = [UIButtonbuttonWithType:UIButtonTypeCustom];
setBT.frame = CGRectMake(kSCreen_width-60,0, 50, 50);
[setBT setImage:[UIImageimageNamed:@"btn_setting"]forState:UIControlStateNormal];
[setBT addTarget:selfaction:@selector(pushSetting)forControlEvents:UIControlEventTouchUpInside];
[_topView addSubview:setBT];
}
return_topView;
}
//跳转到设置界面
-(void)pushSetting{
SetupViewController *setVC = [[SetupViewControlleralloc] init];
[self.navigationControllerpushViewController:setVC animated:YES];
}
//开关灯
-(UIButton *)lightButton{
if (!_lightButton) {
_lightButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
_lightButton.frame =CGRectMake((kSCreen_width-60)/2,kSCreen_height-100,60, 60);
[_lightButtonsetBackgroundImage:[UIImageimageNamed:@"btn_led_off"]forState:UIControlStateNormal];
_lightButton.tag =100;
[_lightButtonaddTarget:selfaction:@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) {
[_lightButtonsetBackgroundImage:[UIImageimageNamed:@"btn_led_on"]forState:UIControlStateSelected];
readerViewZbar.torchMode =1;
}else{
[_lightButtonsetBackgroundImage:[UIImageimageNamed:@"btn_led_off"]forState:UIControlStateSelected];
readerViewZbar.torchMode =0;
}
}
//正确提示
-(UIView *)correctView{
if (!_correctView) {
_correctView = [[UIViewalloc] initWithFrame:CGRectMake(0,kSCreen_height/2,kSCreen_width, kSCreen_height/2)];
_correctView.backgroundColor = [UIColorwhiteColor];
UILabel *yanzhengLabel = [[UILabelalloc] initWithFrame:CGRectMake(0,0, kSCreen_width,kSCreen_height/10-10)];
yanzhengLabel.backgroundColor = [UIColorcolorWithRed:115/255.0green:199/255.0blue:69/255.0alpha:1];
yanzhengLabel.text = @"验证通过";
yanzhengLabel.textColor = [UIColorwhiteColor];
yanzhengLabel.font = [UIFontsystemFontOfSize:20];
yanzhengLabel.textAlignment = NSTextAlignmentCenter;
[_correctView addSubview:yanzhengLabel];
UILabel *ticketLabel = [[UILabelalloc] initWithFrame:CGRectMake(0,kSCreen_height/10,kSCreen_width, 50)];
ticketLabel.text = @"电子票编号";
ticketLabel.textColor = [UIColorlightGrayColor];
ticketLabel.textAlignment = NSTextAlignmentCenter;
[_correctView addSubview:ticketLabel];
UILabel *numLabel = [[UILabelalloc] initWithFrame:CGRectMake(0,kSCreen_height/10+40,kSCreen_width, kSCreen_height/10)];
numLabel.text = [udobjectForKey:@"ticketmsg"];
numLabel.font = [UIFontsystemFontOfSize:22];
numLabel.textAlignment = NSTextAlignmentCenter;
[_correctView addSubview:numLabel];
UIButton *correctButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
correctButton.frame = CGRectMake((kSCreen_width-80)/2,3*kSCreen_height/10,80, 80);
[correctButton setBackgroundImage:[UIImageimageNamed:@"btn_result_pass"]forState:UIControlStateNormal];
[correctButton addTarget:selfaction:@selector(correctBack)forControlEvents:UIControlEventTouchUpInside];
[_correctView addSubview:correctButton];
}
return_correctView;
}
//点击继续扫描
-(void)correctBack{
[self.correctViewremoveFromSuperview];
[self scan];
}
//错误提示
-(UIView *)errorView{
if (!_errorView) {
_errorView = [[UIViewalloc] initWithFrame:CGRectMake(0,kSCreen_height/2,kSCreen_width, kSCreen_height/2)];
_errorView.backgroundColor = [UIColorwhiteColor];
UILabel *yanzhengLabel = [[UILabelalloc] initWithFrame:CGRectMake(0,0, kSCreen_width,kSCreen_height/10-10)];
yanzhengLabel.backgroundColor = [UIColorcolorWithRed:115/255.0green:199/255.0blue:69/255.0alpha:1];
yanzhengLabel.text = @"验证失败";
yanzhengLabel.textColor = [UIColorwhiteColor];
yanzhengLabel.font = [UIFontsystemFontOfSize:20];
yanzhengLabel.textAlignment = NSTextAlignmentCenter;
[_errorView addSubview:yanzhengLabel];
UILabel *ticketLabel = [[UILabelalloc] initWithFrame:CGRectMake(0,kSCreen_height/10,kSCreen_width, 50)];
ticketLabel.text = @"错误原因";
ticketLabel.textColor = [UIColorlightGrayColor];
ticketLabel.textAlignment = NSTextAlignmentCenter;
[_errorView addSubview:ticketLabel];
UILabel *reasonLabel = [[UILabelalloc] initWithFrame:CGRectMake(0,kSCreen_height/10+40,kSCreen_width, kSCreen_height/10)];
reasonLabel.text = [udobjectForKey:@"ticketmsg"];
reasonLabel.font = [UIFontsystemFontOfSize:22];
reasonLabel.textAlignment = NSTextAlignmentCenter;
[_errorView addSubview:reasonLabel];
UIButton *errorButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
errorButton.frame = CGRectMake((kSCreen_width-80)/2,3*kSCreen_height/10,80, 80);
[errorButton setBackgroundImage:[UIImageimageNamed:@"btn_result_error"]forState:UIControlStateNormal];
[errorButton addTarget:selfaction:@selector(errorBack)forControlEvents:UIControlEventTouchUpInside];
[_errorView addSubview:errorButton];
}
return_errorView;
}
//点击继续扫描
-(void)errorBack{
[self.errorViewremoveFromSuperview];
[self scan];
}
//扫描二维码
-(void)scan{
readerViewZbar = [[ZBarReaderViewalloc]init];
readerViewZbar.frame =CGRectMake(0,0, kSCreen_width,kSCreen_height);
readerViewZbar.readerDelegate =self;
//关闭闪光灯
readerViewZbar.torchMode =0;
[readerViewZbaraddSubview:self.topView];
[readerViewZbaraddSubview:self.lightButton];
UILabel *lightLabel = [[UILabelalloc] initWithFrame:CGRectMake((kSCreen_width-60)/2,kSCreen_height-40,60, 30)];
lightLabel.text = @"开灯";
lightLabel.textColor = [UIColorwhiteColor];
lightLabel.textAlignment =NSTextAlignmentCenter;
[readerViewZbar addSubview:lightLabel];
//扫描区域
ZBarImageScanner * scanner =readerViewZbar.scanner;
[scanner setSymbology:ZBAR_I25
config:ZBAR_CFG_ENABLE
to:0];
UIImageView *scanImageView = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"border_scan"]];
scanImageView.frame =CGRectMake((kSCreen_width-kSCreen_height/3)/2,kSCreen_height/3,kSCreen_height/3,kSCreen_height/3);
[readerViewZbar addSubview:scanImageView];
_line = [[UIImageViewalloc] initWithFrame:CGRectMake(0,0,kSCreen_height/3,4)];
_line.image = [UIImageimageNamed:@"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
= [[ZBarCameraSimulatoralloc]initWithViewController:self];
cameraSimulator.readerView = readerViewZbar;
}
[self.viewaddSubview: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];
[readerViewZbarstart];
}
//扫码线动画
-(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 *symbolin symbols) {
NSLog(@"symbol%@", symbol.data);
[ud setObject:symbol.dataforKey:@"symbol"];
break;
}
NSLog(@"haha");
[readerView stop];
[self saoyisao];
}
//扫码网络请求
-(void)saoyisao{
AFHTTPSessionManager *manager = [AFHTTPSessionManagermanager];
manager.requestSerializer = [AFHTTPRequestSerializerserializer];
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"text/html"];
// manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSLog(@"token%@",[udobjectForKey:@"token"]);
NSMutableData *data = [NSMutableDatadataWithData:[NSJSONSerializationdataWithJSONObject:@{@"token":[udobjectForKey:@"token"],@"user_encode":[udobjectForKey:@"symbol"]}options:NSJSONWritingPrettyPrintederror:nil]];
NSString *jsonString = [[NSStringalloc] initWithData:dataencoding: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"];
[readerViewZbaraddSubview:self.correctView];
} else {
NSLog(@"失败");
[ud setObject:[responseObject objectForKey:@"msg"]forKey:@"ticketmsg"];
[readerViewZbaraddSubview:self.errorView];
}
} failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
NSLog(@"Error: %@", error);
MBProgressHUD *hud = [MBProgressHUDshowHUDAddedTo:self.viewanimated:YES];
hud.delegate = self;
hud.backgroundColor = [UIColorclearColor];
hud.labelText = @"网络异常";
hud.margin = 10.f;
hud.yOffset = 100.f;
hud.removeFromSuperViewOnHide =YES;
[hud hide:YESafterDelay:1];
[self scan];
}];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
_line.frame =CGRectMake(30,10, 180, 4);
//num = 0;
upOrdown = NO;
readerViewZbar.torchMode =0;
[readerViewZbarstop];
count = 0;
[timerinvalidate];
timer = nil;
[self.correctViewremoveFromSuperview];
[self.errorViewremoveFromSuperview];
}
- (void)dealloc
{
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// 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
- 自定义ZbarView
- 自定义
- 自定义
- 自定义
- 自定义
- 自定义
- 自定义TexBox,自定义ComboBox
- 自定义View自定义属性
- 自定义View自定义属性
- 自定义View自定义属性
- 自定义View自定义属性
- 自定义一个自定义对话框
- 自定义控件-自定义吐司
- 自定义checkbox,自定义button
- 自定义控件--自定义对话框
- 自定义控件----自定义属性
- 自定义控件:自定义Toast
- 自定义View自定义属性
- vijosP1413 Valentine’s Present
- HTTPS学习(一):工作过程
- vijosP1371 方程的解
- vijosP1388 二叉树数
- vijosP1687 细菌总数
- 自定义ZbarView
- vijosP1629 八
- BZOJ1008 [HNOI2008]越狱
- 洛谷1349 广义斐波那契数列 【矩阵乘法】
- 洛谷1076 寻宝
- BZOJ2705 [SDOI2012]Longge的问题
- 洛谷P1407 工资
- Spring MVC系列(一)之Hello World
- 洛谷1775 古代人的难题