iOS经典讲解之实现扫描二维码ZBarSDK的使用

作者:Loving_iOS

转载请标明出处:http://blog.csdn.net/loving_ios/article/details/49872529

ZBarSDK,一个比较优秀的开源项目,使用起来也很简单。

ZBarSDK是一个开源的SDK,可从这里下载到源码,该SDK实现了识别和读取各种条形码,包括EAN-13/UPC-A, UPC-E, EAN-8, Code 128, Code 39, Interleaved 2 of 5 和 QR Code。

帮助文档:http://zbar.sourceforge.net/iphone/sdkdoc/index.html

Step1.使用ZBarSDK 需要导入的framework

1.AVFoundation.framework

2.CoreMedia.framework

3.CoreVideo.framework

4.QuartzCore.framework

5.libiconv.dylib

Step2.在ViewController.h 导入#import "ZBarSDK.h"

Step3.在ViewController.h 继承 <ZBarReaderDelegate>协议

Step4.写代码:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

ViewController.m

#import "ViewController.h"
#import "ZBarSDK.h"

@interface ViewController ()<ZBarReaderDelegate>

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 600, self.view.bounds.size.width, 30);
    [button setTitle:@"扫描" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor greenColor];
    [button addTarget:self action:@selector(scan:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
   self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(110, 50, 100, 100)];
    self.imageView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.imageView];

    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, 320, 50)];
    self.label.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.label];
}

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

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    // ADD: get the decode results
    id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        // EXAMPLE: just grab the first barcode
        break;
    
    // EXAMPLE: do something useful with the barcode data
    self.label.text = symbol.data;
    
    // EXAMPLE: do something useful with the barcode image
    self.imageView.image =
    [info objectForKey: UIImagePickerControllerOriginalImage];
    
    // ADD: dismiss the controller (NB dismiss from the *reader*!)
    [picker dismissViewControllerAnimated:YES completion:nil];

}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (YES);
}
@end
注意:如果出现下面的问题

解决方法很简单:

是静态库不支持arm64,将Valid Architectures中得arm64去掉,然后将build Active Architectre only 设置为NO,就可以了

如图:


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值