ios 使用ZBar读取条形码和二维码

1.使用ZBar项目。下载地址是: http://zbar.sourceforge.net/iphone/index.html

2.新建一个项目。

3.导入 ZBar的sdk。把ZBar SDK的目录拉入项目,然后选中copy选项(见附件)

4.在项目文件的target中加入 以下framework


 

5、.h代码为:

#import <UIKit/UIKit.h>

#import "ZBarSDK.h"

 

@protocol ScanCodeViewDelegate <NSObject>

 

-(void)finishRead:(NSString *)barCode;

 

@end

 

@interface ScanCodeViewController : UIViewController<ZBarReaderViewDelegate>{

    

    id<ScanCodeViewDelegate> delegate;

    

    UIImageView *lineView;

    

}

 

@property (weak, nonatomic) IBOutlet ZBarReaderView *readerView;

 

@property(assign,nonatomic) id<ScanCodeViewDelegate> delegate;

 

 

 

@end

 

6、.m代码为:

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.title=@"条形码扫描";

    

    self.navigationItem.rightBarButtonItem = [ViewUitls getRightUIBarButtonItem:@"取消" target:self action:@selector(cancelScan)];

    

    _readerView.readerDelegate = self;

    //关闭闪光灯

    _readerView.torchMode = 0;

    //取消手动对焦

    _readerView.allowsPinchZoom=NO;

    

    //扫描区域,这里可以自己调整

    CGRect scanMaskRect = CGRectMake(100, CGRectGetMidY(_readerView.frame) - 126, 250, 250);

    

    //处理模拟器

    if (TARGET_IPHONE_SIMULATOR) {

        ZBarCameraSimulator *cameraSimulator

        = [[ZBarCameraSimulator alloc]initWithViewController:self];

        cameraSimulator.readerView = _readerView;

    }

    [self.view addSubview:_readerView];

    //扫描区域计算

    

    CGRect scanCrop=[self getScanCrop:scanMaskRect readerViewBounds:self.readerView.bounds];

    _readerView.scanCrop = scanCrop;

    

   //这里添加了一个类似微信的扫描框,可以根据自己需要添加

    UIImageView *imageView=[[UIImageView alloc] init];

    if(kIsIphone5){

        imageView.image=[UIImage imageNamed:@"scan_bg_568h"];

        imageView.frame=CGRectMake(0, 0, 320, 568);

        

    }else{

        imageView.image=[UIImage imageNamed:@"scan_bg"];

        imageView.frame=CGRectMake(0, 0, 320, 480);

 

    }

    

    [self.view addSubview:imageView];

    

    //这里添加了类似微信的扫描线,并开始上线滑动动画

    lineView=[[UIImageView alloc] initWithFrame:CGRectMake(51, 200, 219, 3)];

    lineView.image=[UIImage imageNamed:@"scan_line"];

    [self.view addSubview:lineView];

    

    [self loadAnimationStart];

    

 

}

 

 

-(void)loadAnimationStart{

    

    if(kIsIphone5){

    

        [UIView animateWithDuration:2.0 animations:^{

            lineView.frame=CGRectMake(lineView.frame.origin.x, 390,lineView.frame.size.width,lineView.frame.size.height);

        } completion:^(BOOL finished) {

            

            [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(secStart) userInfo:nil repeats:NO];

        }];

    }else{

        

        [UIView animateWithDuration:2.0 animations:^{

            lineView.frame=CGRectMake(lineView.frame.origin.x, 345,lineView.frame.size.width,lineView.frame.size.height);

        } completion:^(BOOL finished) {

            

            [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(secStart) userInfo:nil repeats:NO];

        }];

        

    }

    

}

 

-(void)secStart{

    

    if(kIsIphone5){

        

        lineView.frame=CGRectMake(lineView.frame.origin.x, 175,lineView.frame.size.width,lineView.frame.size.height);

        [self loadAnimationStart];

        

    }else{

        

        lineView.frame=CGRectMake(lineView.frame.origin.x, 130,lineView.frame.size.width,lineView.frame.size.height);

        [self loadAnimationStart];

        

        

    }

    

}

 

 

-(void)cancelScan{

    

    [_readerView stop];

    [self dismissViewControllerAnimated:YES completion:nil];

    

}

 

 

 

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

    returnCGRectMake(x, y, width, height);

 

}

 

-(void)viewDidAppear:(BOOL)animated{

    

    [super viewDidAppear:YES];

    [_readerView start];

}

 

 

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

{

    NSString *str;

    

    for (ZBarSymbol *symbol in symbols) {

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

        str=symbol.data;

        break;

    }

    

    

    [_readerView stop];

    

    if([_delegate respondsToSelector:@selector(finishRead:)]){

        

        [_delegate finishRead:str];

    }

    

    [self dismissViewControllerAnimated:YES completion:nil];

    

 

}

 

//代码见附件,直接导入工程就可以使用了

你可能感兴趣的:(ios)