添加依赖库:AVFoundation.framework、libiconv.2.dylib、CoreMedia.framework、CoreVideo.framework,其中libzbar.a是SDK中的,(如果需要通过SVN上传自己代码时,要记得还有这个.a文件)
在你要进行添加扫描的界面的.h里可直接复制到工程里
添加头文件
import "ZBarSDK.h"
//设置代理
@protocol ErweimaDelegate
//扫描出结果则调用这个方法
-(void)finishRead:(NSString *)barCode;
@end
@interface ErweimaViewController : superVC < ZBarReaderViewDelegate > //此处superVC是自定义的父类
@property (strong, nonatomic)id < ErweimaDelegate >delegate;
@end
4.在.m文件中:
@interface ErweimaViewController ()
{
ZBarReaderViewrederView;//二维码扫描View,可自定义样式
UIImageViewlineView;//自定义的扫描线
NSTimer *time;//定时器,控制扫描线跑动
}
//在进入界面时就开始扫描
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];[rederView start];
}
//退出界面时就停止扫描
(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[rederView stop];
}
-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"扫描";
[self initView];
}
(void)initView
{
rederView = [[ZBarReaderView alloc] init];
rederView.frame = CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height);
rederView.readerDelegate = self;
rederView.allowsPinchZoom = NO; // 不使用Pinch手势变焦
rederView.torchMode = 0;//闪光灯 关
rederView.trackingColor = [UIColor clearColor];//锁定图码 显示的颜色框
if (TARGET_IPHONE_SIMULATOR) {
ZBarCameraSimulatorcamera = [[ZBarCameraSimulator alloc] initWithViewController:self];
camera.readerView = rederView;
}
[self.view addSubview:rederView];
/************可忽略此域中的以下代码,可以自己自定义***************/
//扫描区域计算
CGRect scanMaskRect = CGRectMake(Width_Screen/2-75, 150, 150, 150);
CGRect scanCrop=[self getScanCrop:scanMaskRect readerViewBounds:rederView.bounds];
rederView.scanCrop = scanCrop;
[rederView.captureReader captureFrame];
[rederView start];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(-20, -64, Width_Screen+40, Height_Screen)];
image.image = [UIImage imageNamed:@"111111111"];
image.backgroundColor = [UIColor clearColor];
image.alpha = 0.3;
[self.view addSubview:image];
//这里添加了类似微信的扫描线,并开始上线滑动动画
lineView=[[UIImageView alloc] initWithFrame:CGRectMake(51, 200, 219, 3)];
// lineView.image=[UIImage imageNamed:@"scan_line"];
lineView.backgroundColor = UIColor_Theme;
[self.view addSubview:lineView];
[self loadAnimationStart];
}
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)rvBounds{
CGFloat x,y,width,height;
x = rect.origin.y / rvBounds.size.height;
y = 1 - (rect.origin.x + rect.size.width) / rvBounds.size.width;
width = (rect.origin.y + rect.size.height) / rvBounds.size.height;
height = 1 - rect.origin.x / rvBounds.size.width;
return CGRectMake(x, y, width, height);
}
-(void)loadAnimationStart{
if(Width_Screen !=320){ [UIViewanimateWithDuration:2.0animations:^{ lineView.frame=CGRectMake(lineView.frame.origin.x,390,lineView.frame.size.width,lineView.frame.size.height); }completion:^(BOOL finished) { time = [NSTimerscheduledTimerWithTimeInterval:0.2target:selfselector:@selector(secStart)userInfo:nilrepeats:NO]; }];}else{ [UIViewanimateWithDuration:2.0animations:^{ lineView.frame=CGRectMake(lineView.frame.origin.x,345,lineView.frame.size.width,lineView.frame.size.height); }completion:^(BOOL finished) { time = [NSTimerscheduledTimerWithTimeInterval:0.2target:selfselector:@selector(secStart)userInfo:nilrepeats:NO]; }]; }
}
-(void)secStart{
if(Width_Screen !=320){ lineView.frame=CGRectMake(lineView.frame.origin.x,175,lineView.frame.size.width,lineView.frame.size.height);if(time !=nil) { [selfloadAnimationStart]; } }else{ lineView.frame=CGRectMake(lineView.frame.origin.x,130,lineView.frame.size.width,lineView.frame.size.height);if(time !=nil) { [selfloadAnimationStart];NSLog(@"123"); }NSLog(@"qwe");}
}
-(void)readerView:(ZBarReaderView)readerView didReadSymbols:(ZBarSymbolSet)symbols fromImage:(UIImage)image
{
NSStringstr;//扫描出的字段,记录下来
for(ZBarSymbol *symbolinsymbols) {NSLog(@"%@", symbol.data); str=symbol.data; [rederView stop]; [self.navigationController popViewControllerAnimated:YES]; [selfshowHudMessageAndHidden:str];//展示出扫描结果,根据需求可自定义展示样式lineView.hidden =YES; [time setFireDate:[NSDatedistantFuture]]; [time invalidate]; time =nil;break;}//这个代理是 返回到上个界面把str传过去的,根据需求可改变if([_delegate respondsToSelector:@selector(finishRead:)]){ [_delegate finishRead:str];}