用Zbar扫描二维码

一、准备工作

1.最近碰到一个二维码(下图所示),用原生的方法和ZXing都扫不出来,最后用Zbar扫出来了,所以专门介绍一下这个。

3668714B-DA34-4B27-AAFC-8382DBDE3526.png

2.添加依赖

屏幕快照 2016-11-20 上午12.15.59.png

3.设置bitcode为NO.

二、扫描代码

#import "ViewController.h"
#import "ZBarSDK.h"
@interface ViewController ()<ZBarReaderDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化相机控制器
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
    reader.view.frame = self.view.bounds;
    [self.view addSubview:reader.view];
    //设置代理
    reader.readerDelegate = self;
    //基本适配
//    reader.supportedOrientationsMask = ZBarOrientationMaskAll;
    //二维码/条形码识别设置
    ZBarImageScanner *scanner = reader.scanner;
    [scanner setSymbology: ZBAR_I25
                   config: ZBAR_CFG_ENABLE
                       to: 0];
    //弹出系统照相机,全屏拍摄
    [self presentViewController:reader animated:YES completion:^{
        
    }];

}

#pragma mark -
#pragma mark ZBarReaderDelegate
//扫描二维码的时候,识别成功会进入此方法,读取二维码内容
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
    ZBarSymbol * symbol;
    for(symbol in results)
        break;
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    NSString *result = symbol.data;
    
    NSLog(@"%@",result);
    
    //二维码扫描成功,弹窗提示
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"扫描成功" message:[NSString stringWithFormat:@"二维码内容:\n%@",result] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    [alertVC addAction:action];
    [self presentViewController:alertVC animated:YES completion:^{
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容