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,就可以了

如图:


你可能感兴趣的:(二维码,ios开发,ZBarSDK)