1.首先下载ZBarSDK,下载地址
2.导入sdk ,并导入下面的依赖库
3.下面是代码执行
@protocol SaoyisaoDelegate <NSObject>
-(void)saoyisaojieguo:(NSString *)data;
@end
@interface SaoyisaoController : UIViewController
@property(nonatomic,assign)id<SaoyisaoDelegate>delegate;
@end
这是.h 文件
#import "SaoyisaoController.h"
#import <AVFoundation/AVFoundation.h>
#import "ZBarSDK.h"
@interface SaoyisaoController ()<ZBarReaderViewDelegate>
{
AVCaptureMetadataOutput *captureMetadataOutput ;
ZBarReaderView *readerView;
ZBarCameraSimulator *cameraSim;
NSTimer *timer;
}
@property (retain, nonatomic) UIView *viewPreview;
@property (retain, nonatomic) UILabel *lblStatus;
@property (retain, nonatomic) UIButton *startBtn;
@property (retain, nonatomic) UIView *boxView;
@property (nonatomic) BOOL isReading;
@property (retain, nonatomic) CALayer *scanLayer;
//捕捉会话
@property (nonatomic, retain) AVCaptureSession *captureSession;
//展示layer
@property (nonatomic, retain) AVCaptureVideoPreviewLayer *videoPreviewLayer;
@end
@implementation SaoyisaoController
- (void)viewDidLoad
{
[super viewDidLoad];
[self cerateNavtabbar];
[self createSaoma];
}
-(void)cerateNavtabbar
{
UIButton *imageView1 = [UIButton buttonWithType:UIButtonTypeSystem];
imageView1.frame = CGRectMake(0, 0, 30, 30);
imageView1.tag=1101;
[imageView1 addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
[imageView1 setBackgroundImage:[UIImage imageNamed:@"saosao_fanhui"] forState:0];
UIBarButtonItem *item5 = [[UIBarButtonItem alloc] initWithCustomView:imageView1];
item5.tag=1101;
self.navigationItem.leftBarButtonItem=item5 ;
}
-(void)createSaoma
{
readerView=[[ZBarReaderView alloc]init];
readerView.frame=self.view.bounds;
readerView.readerDelegate = self;
_boxView = [[UIView alloc] initWithFrame:CGRectMake(readerView.bounds.size.width * 0.2f, readerView.bounds.size.height * 0.2f-20, readerView.bounds.size.width - readerView.bounds.size.width * 0.4f, readerView.bounds.size.height - readerView.bounds.size.height * 0.5f)];
_boxView.layer.borderColor = [UIColor greenColor].CGColor;
_boxView.layer.borderWidth = 1.0f;
[readerView addSubview:_boxView];
//10.2.扫描线
_scanLayer = [[CALayer alloc] init];
_scanLayer.frame = CGRectMake(0, 0, _boxView.bounds.size.width, 1);
_scanLayer.backgroundColor = [UIColor whiteColor].CGColor;
[_boxView.layer addSublayer:_scanLayer];
timer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(moveScanLayer:) userInfo:nil repeats:YES];
[timer fire];
[self.view addSubview:readerView];
[readerView setAllowsPinchZoom:YES];
if (TARGET_IPHONE_SIMULATOR) {
cameraSim = [[ZBarCameraSimulator alloc] initWithViewController:self];
cameraSim.readerView = readerView;
}
UILabel *label2=[Helper justLabelWithfram:CGRectMake(0, 20, self.view.bounds.size.width, 40) size:17];
label2.textColor=[UIColor whiteColor];
label2.backgroundColor=[UIColor clearColor];
label2.text=@"扫码获取商品详细信息";
label2.textAlignment=NSTextAlignmentCenter;
[self.view addSubview:label2];
UILabel *label3=[Helper justLabelWithfram:CGRectMake(0, self.view.bounds.size.height-150, self.view.bounds.size.width, 30) size:15];
label3.textColor=[UIColor whiteColor];
label3.backgroundColor=[UIColor clearColor];
label3.text=@"请把条形码,二维码对准框内";
label3.textAlignment=NSTextAlignmentCenter;
[self.view addSubview:label3];
}
-(void)itemClick:(UIBarButtonItem *)item
{
[self.navigationController popViewControllerAnimated:YES];
}
-(void)viewDidAppear:(BOOL)animated
{
[readerView start];
}
-(void)viewDidDisappear:(BOOL)animated
{
[readerView stop];
}
-(void) readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
NSString *codeData = [[NSString alloc] init];;
for (ZBarSymbol *sym in symbols) {
codeData = sym.data;
break;
}
[self.delegate saoyisaojieguo:codeData];
[self.navigationController popToRootViewControllerAnimated:YES];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// 得到条形码结果
id<NSFastEnumeration> results =
[info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)
break;
//获得到条形码
//NSString *dataNum=symbol.data;
//扫描界面退出
// [picker dismissModalViewControllerAnimated: YES];
}
- (void)moveScanLayer:(NSTimer *)timer
{
CGRect frame = _scanLayer.frame;
if (_boxView.frame.size.height <= _scanLayer.frame.origin.y) {
frame.origin.y = 0;
_scanLayer.frame = frame;
// captureMetadataOutput.rectOfInterest=CGRectMake(0.5,0,0.5, 1);
}else{
frame.origin.y += 5;
[UIView animateWithDuration:0.1 animations:^{
_scanLayer.frame = frame;
}];
}
}
-(void)dealloc
{
[timer setFireDate:[NSDate distantFuture]];
}
@end
这是.m文件
4.在要扫码的视图里 实现扫码的代理
-(void)saoyisaojieguo:(NSString *)data
{
NSLog(@"%@",data);
//根据data值处理响应的处理
if (data.length==12) {
//有些快递单的12位
}else if(data.length==13)
{
//一般条形码都是13位
}else if ([data rangeOfString: @"qr.weibo.cn"].location>0)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:data]];
}
}