在我们开发ios应用时,尤其是电子商务类应用,时常遇到条形码扫描的业务需求,幸运的已经有开源的SDK供我们使用──条形码的SDK for ios,下面介绍一下这个开源的SDK的使用方法:
免费的条形码是zbar, 下载地址是:https://github.com/samrtian/ZBar
具体的实现过程如下:
1.把下载的ZBarSDK添加到工程中。
2.添加库文件
AVFoundation.framework CoreMedia.framework CoreVideo.framework libiconv.dylib libzbar.a
3. 加入头文件和协议
#import "ZBarSDK/ZBarReaderViewController.h" 和协议 ZBarReaderDelegate
4.代码实现
_zBar = [ZBarReaderViewController new];
_zBar.readerDelegate = self;
ZBarImageScanner *scanner = _zBar.scanner;
[scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0]; //设置扫描参数
[self presentModalViewController:_zBar animated: YES];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
id<NSFastEnumeration> result = [info objectForKey:ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for (symbol in result)
break;
NSString *str = symbol.data; //这里就是条码
[_zBar dismissModalViewControllerAnimated:YES];
}