1. 下载SDK https://github.com/bmorton/ZBarSDK
2. 引用到项目中
3. 添加引用
4. AppDelegate中添加下面代码
5. 在需要使用扫描的controller中实现ZBarReaderViewDelegate
6. 对应的m文件中
#import "ViewController.h"
@interface ViewController ()
{
UITextField *resultText;
ZBarReaderView *readerView;
ZBarCameraSimulator *cameraSim;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
resultText = [[UITextField alloc] initWithFrame:CGRectMake(20, 40, 200, 60)];
resultText.backgroundColor = [UIColor blueColor];
[self.view addSubview:resultText];
readerView = [[ZBarReaderView alloc]init];
readerView.frame = CGRectMake(10, 110, 300, 300);
readerView.readerDelegate = self;
//关闭闪光灯
readerView.torchMode = 0;
//处理模拟器
if (TARGET_IPHONE_SIMULATOR) {
ZBarCameraSimulator *cameraSimulator
= [[ZBarCameraSimulator alloc]initWithViewController:self];
cameraSimulator.readerView = readerView;
}
[self.view addSubview:readerView];
[readerView start];
}
- (void) readerView: (ZBarReaderView*) view
didReadSymbols: (ZBarSymbolSet*) syms
fromImage: (UIImage*) img
{
// do something useful with results
for(ZBarSymbol *sym in syms) {
resultText.text = sym.data;
break;
}
[readerView stop];
}