用Zbar扫描二维码

一、准备工作

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

用Zbar扫描二维码_第1张图片
3668714B-DA34-4B27-AAFC-8382DBDE3526.png

2.添加依赖

用Zbar扫描二维码_第2张图片
屏幕快照 2016-11-20 上午12.15.59.png

3.设置bitcode为NO.

用Zbar扫描二维码_第3张图片

二、扫描代码

#import "ViewController.h"
#import "ZBarSDK.h"
@interface ViewController ()

@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 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

你可能感兴趣的:(用Zbar扫描二维码)