ZBar 二维码扫描相关

ZBar
把ZBar 那个demo完善
ZBarReaderViewController 的属性
showsZBarControls:是否展示tooBar 上面有 cansel 和感叹号按钮

if !TARGET_IPHONE_SIMULATOR

code

endif

当不是模拟器的时候

两种方式去展示扫描二维码
1.ZBarReaderView
自定义ViewController, 在ViewController 上加上 ZBarReaderView,通过ZBarReaderView展示
步骤
ZBarImageScanner *scanner = [[ZBarImageScanner alloc] init];
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
ZBarReaderView *readerView = [[ZBarReaderView alloc] initWithImageScanner:scanner];
readerView.frame = CGRectMake(100, 100, 200, 200);
readerView.readerDelegate = self;
readerView.torchMode = 0;
readerView.showsFPS = YES;
[self.view addSubview:readerView];
[readerView start];

代理方法获取扫描的二维码数据

  • (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image{
    ZBarSymbol *symbol = nil;
    for (symbol in symbols) {
    break;
    }

    NSLog(@"%@",symbol.data);
    }

2.ZBarReaderViewController 展示
最简单的方法,就是直接push到ZBarReaderViewController
步骤
//初始化相机控制器
ZBarReaderViewController *reader = [ZBarReaderViewController new];
//设置代理

reader.showsZBarControls = YES;
reader.tracksSymbols = YES;

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:nil];
// [self]

代理方法获取二维码信息

  • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    // ADD: get the decode results
    id results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
    // EXAMPLE: just grab the first barcode
    break;

    NSLog(@"%@",symbol.data);
    // ADD: dismiss the controller (NB dismiss from the reader!)
    [picker dismissViewControllerAnimated:YES completion:nil];
    }

Zbar是日本人的项目,有可能会出现乱码的情况,解决办法
NSString * str = [NSString stringWithCString:[symbol.data cStringUsingEncoding: NSShiftJISStringEncoding] encoding:NSUTF8StringEncoding];

ZBar 是二维码扫描灵敏度最高的库

以后凡是遇到要经过用户授权的操作时,一定要判断是否有该操作的权限,如是否有定位权限,是否有打开相机,相册,通讯录权限等!

LBXScanDemo
是否有相册权限
runtime 发送消息

//判断iPhone和iPad

define IS_IPHONE (!IS_IPAD)

define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)

//判断设备版本

define IS_IOS6_LAGACY floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1

define IS_IOS7 floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1

define IS_IOS5_LAGACY floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_5_1

define IS_IOS6_AFTER floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_5_1

关于命名:
ZXingWrapper 是对ZXing扫码库的 封装, 库名+封装 ZXing+Wrapper

你可能感兴趣的:(ZBar 二维码扫描相关)