准备工作:
1.下载ZBarSDKhttps://github.com/bmorton/ZBarSDK,加入到项目中
2.导入如下框架
3.导入#import "ZBarSDK.h"
4.继承协议@interface ViewController :UIViewController<ZBarReaderDelegate,ZBarReaderViewDelegate>
扫描步骤:
1.通过摄像头获取图片
2.通过Zbar的内部处理方法来识别图片
3.处理信息并显示
代码实现:
1.创建视图
-(void)creatView{
//用于响应扫描事件,点击开始扫描
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(110, 200, 100, 40)];
[button setTitle:@"扫描" forState:UIControlStateNormal];
[button addTarget:self action:@selector(scanButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//用于显示扫描到的图像
imageview = [[UIImageView alloc]initWithFrame:CGRectMake(20, 50, 280, 280)];
[self.view addSubview:imageview];
//用于显示扫描到的信息
label = [[UILabel alloc]initWithFrame:CGRectMake(20, 356, 280, 68)];
[self.view addSubview:label];
}
2.扫描二维码
方法一:直接调用ZBar提供的ZBarReaderViewController打开一个扫描界面
step1:扫描二维码操作
-(void)scanButtonPressed:(id)sender{
ZBarReaderViewController *reader = [[ZBarReaderViewController alloc]init];
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];
}
step2:找到二维码回调的时候会执行ZBarReaderDelegate的对应方法
- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info{
//通过info获得结果
id<NSFastEnumeration> results =
[info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)
break;
<span style="white-space:pre"> </span>
imageview.image =
[info objectForKey: UIImagePickerControllerOriginalImage];
//扫描界面退出
[reader dismissViewControllerAnimated:YES completion:nil];
}
方法二:使用ZBar提供的可以嵌在其他视图中的ZBarReaderView
step1:扫描二维码操作
-(void)scanButtonPressed:(id)sender{
ZBarReaderView *readerView = [[ZBarReaderView alloc]init];
readerView.frame = CGRectMake(10, 44, 300, 300);
readerView.readerDelegate = self;
//扫描区域
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];
//调用ZBarReaderView的start方法开始扫描
[readerView start];
}
step2:扫描区域计算
-(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);
}
step3:在扫描到二维码回调的时候会执行ZBarReaderViewDelegate的对应方法
<span style="font-size:18px;">- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
for (ZBarSymbol *symbol in symbols) {
NSLog(@"%@", symbol.data);
//将信息显示在lable上
label.text = symbol.data ;
break;
}
//调用ZBarReaderView的stop方法停止扫描
[readerView stop];
}</span>