首先通过cocoapods来
搜索 (pod search ZBarSDK )
安装(pod 'ZBarSDK', '~> 1.3.1')
ZBarSDK
然后我们开始开发调试:
引入头文件:
#import <ZBarSDK.h>
多说不宜直接上代码,复制下来就可以用。
在使用ZBarReaderViewController时
添加代理ZBarReaderDelegate
- (void)scan:(id)sender {
ZBarReaderViewController * reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
ZBarImageScanner * scanner = reader.scanner;
[scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
reader.showsZBarControls = YES;
[self presentViewController:reader animated:YES completion:nil];
}
实现ZBarReaderDelegate的代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
id<NSFastEnumeration> results=[info objectForKey:ZBarReaderControllerResults];
ZBarSymbol * symbol;
for(symbol in results)
break;
/**
* 扫描框拍摄到的扫描区域
*/
UIImageView *imageview=[[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.tableView addSubview:imageview];
imageview.image=[info objectForKey:UIImagePickerControllerOriginalImage];
/**
* 扫描到的内容
*/
NSLog(@"zbar===----/%@/",symbol.data);
/**
* 结束扫描
*/
[picker dismissViewControllerAnimated:YES completion:nil];
}
添加代理ZBarReaderViewDelegate
- (void)viewDidLoad
{
[super viewDidLoad];
readerView = [[ZBarReaderView alloc]init];
readerView.frame = CGRectMake(0, 100, 100, 100);
readerView.readerDelegate = self;
//关闭闪光灯
readerView.torchMode = 0;
//扫描区域
CGRect scanMaskRect = CGRectMake(60, CGRectGetMidY(readerView.frame) - 126, 200, 200);
//处理模拟器
if (TARGET_IPHONE_SIMULATOR) {
ZBarCameraSimulator *cameraSimulator
= [[ZBarCameraSimulator alloc]initWithViewController:self];
cameraSimulator.readerView = readerView;
}
[self.view addSubview:readerView];
//扫描区域计算
readerView.scanCrop = [self getScanCrop:scanMaskRect readerViewBounds:readerView.bounds];
[readerView start];
}
//扫描区域计算
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds
{
CGFloat x,y,width,height;
x = rect.origin.x / readerViewBounds.size.width;
y = rect.origin.y / readerViewBounds.size.height;
width = rect.size.width / readerViewBounds.size.width;
height = rect.size.height / readerViewBounds.size.height;
return CGRectMake(x, y, width, height);
}
ZBarReaderViewDelegate的实现
- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
for (ZBarSymbol *symbol in symbols) {
NSLog(@"%@", symbol.data);
break;
}
[readerView stop];
}
使用iPhone自带框架做二维码扫描, 参考 http://www.tuicool.com/articles/IzINZnQ