记录一下二维码扫描的开发 ZBarSDK

本文介绍如何利用ZBarSDK在iOS应用中实现二维码扫描功能。文章详细讲解了通过CocoaPods安装ZBarSDK的过程,并提供了使用ZBarReaderViewController和ZBarReaderView进行扫描的示例代码。
摘要由CSDN通过智能技术生成

首先通过cocoapods来

搜索 (pod search ZBarSDK )

安装(pod 'ZBarSDK', '~> 1.3.1')

ZBarSDK


然后我们开始开发调试:

引入头文件:

#import <ZBarSDK.h>

多说不宜直接上代码,复制下来就可以用。


在使用ZBarReaderViewController时

添加代理ZBarReaderDelegate


- (void)scan:(id)sender {
    ZBarReaderViewController * reader = [ZBarReaderViewController new];
    reader.readerDelegate = self;
    ZBarImageScanner * scanner = reader.scanner;
    [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
    reader.showsZBarControls = YES;
    [self presentViewController:reader animated:YES completion:nil];
}

 实现ZBarReaderDelegate的代理方法 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    id<NSFastEnumeration> results=[info objectForKey:ZBarReaderControllerResults];
    ZBarSymbol * symbol;
    for(symbol in results)
        break;
    /**
     *  扫描框拍摄到的扫描区域
     */
    UIImageView *imageview=[[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [self.tableView addSubview:imageview];
    imageview.image=[info objectForKey:UIImagePickerControllerOriginalImage];
    /**
     *  扫描到的内容
     */
    NSLog(@"zbar===----/%@/",symbol.data);
    /**
     *  结束扫描
     */
    [picker dismissViewControllerAnimated:YES completion:nil];
}


使用ZBarReaderView时

添加代理ZBarReaderViewDelegate


- (void)viewDidLoad
{
    [super viewDidLoad];
    readerView = [[ZBarReaderView alloc]init];
    readerView.frame = CGRectMake(0, 100, 100, 100);
    readerView.readerDelegate = self;
    //关闭闪光灯
    readerView.torchMode = 0;
    //扫描区域
    CGRect scanMaskRect = CGRectMake(60, CGRectGetMidY(readerView.frame) - 126, 200, 200);
    
    //处理模拟器
    if (TARGET_IPHONE_SIMULATOR) {
        ZBarCameraSimulator *cameraSimulator
        = [[ZBarCameraSimulator alloc]initWithViewController:self];
        cameraSimulator.readerView = readerView;
    }
    [self.view addSubview:readerView];
    //扫描区域计算
    readerView.scanCrop = [self getScanCrop:scanMaskRect readerViewBounds:readerView.bounds];
    
    [readerView start];
}

//扫描区域计算
-(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);
}


ZBarReaderViewDelegate的实现

- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
    for (ZBarSymbol *symbol in symbols) {
        NSLog(@"%@", symbol.data);
        break;
    }
    
    [readerView stop];
}


使用iPhone自带框架做二维码扫描, 参考 http://www.tuicool.com/articles/IzINZnQ


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值