1.下载ZBar SDK并导入工程
地址:https://github.com/bmorton/ZBarSDK
2.往项目中添加Framework框架及动态库
AVFoundation.framework
CoreMedia.framework
CoreVideo.framework
libiconv.dylib
libber.a
3.在使用的地方引入头文件
#import "ZBarSDK.h"
4.使用 ZBarReaderViewController扫描二维码
扫描:
- (void)scan:(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];
reader.showsZBarControls = YES; [self presentViewController:reader animated:YES completion:nil];
}
现在获取二维码的相关信息:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo(NSDictionary *)info{
id<NSFastEnumeration>results = [info objectForKey:ZBarReaderControllerResults];
ZBarSymbol *symbol = nil; //代码如何修改
for (symbol in results) break; //symbol = results;
imageVIew.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
_label.text = symbol.data;
NSLog(@"扫描内容:%@",symbol.data);
}
5.使用 ZBarReaderView实现扫描页面高度订制
实现协议 ZBarReaderViewDelegate (UIAlertView)
并定义: ZBarReaderView *readerVIew;
ZBarCameraSimulator *cameraSim;
实现代码:
readerVIew = [[ZBarReaderView alloc]init]; readerVIew.frame = CGRectMake(50, 44, 200, 200); readerVIew.readerDelegate = self; //关闭闪光灯 readerVIew.torchMode = 0; //扫描区域:很重要 CGRect scanMaskRect = CGRectMake(60, CGRectGetMidY(readerVIew.frame)-126, 200, 200); //[readerVIew setAllowsPinchZoom:YES]; if (TARGET_IPHONE_SIMULATOR)//应为是否有权限? { cameraSim = [[ZBarCameraSimulator alloc]initWithViewController:self]; cameraSim.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{ NSString *codeData = [[NSString alloc]init]; for (ZBarSymbol *sym in symbols) { codeData = sym.data; break; } //可以获取到 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:codeData delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; alert.delegate = self; [alert show]; [readerVIew stop];
}
打开获取到的信息:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ [[UIApplication sharedApplication]openURL:[NSURL URLWithString:alertView.message]];
}
6.使用libqrencode生成二维码
步骤一:首先向工程中导入第三方库libqrencode
步骤二:向QRCodeGenerator.h或者整个项目中引入框架 #import <UIKit/UIKit.h>,否则会报错
步骤三:在相应的文件中引入头文件
#import "QRCodeGenerator.h"
步骤四:调用库中的方法生成图片(参数为字符串和图片大小)
UIImage *image = [QRCodeGenerator qrImageForString:_label.text imageSize:imageVIew.bounds.size.width];
其他应用待进一步挖掘。