一、准备工作
1.最近碰到一个二维码(下图所示),用原生的方法和ZXing都扫不出来,最后用Zbar扫出来了,所以专门介绍一下这个。
2.添加依赖
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