网上的例子 zbar 下载地址 http://pan.baidu.com/share/link?shareid=2652605686&uk=923776187
1.新建一个工程A:view-base 2.拖动zbar框架到工程目录下 3.然后把zbar/iphone目录下的工程.xproj文件拖到A 4.在工程的Link Binary添加框架如下:
Step1.使用ZBarSDK 需要导入的framework
1.AVFoundation.framework
2.CoreMedia.framework
3.CoreVideo.framework
4.QuartzCore.framework
5.libiconv.dylib
Step2.在ViewController.h 导入#import "ZBarSDK.h"
Step3.在ViewController.h 继承 <ZBarReaderDelegate>协议
Step4.写代码:
-(IBAction) startScan { ZBarReaderController *reader = [[[ZBarReaderController alloc] init] autorelease]; reader.delegate = self; reader.cameraMode = ZBarReaderControllerCameraModeSampling; ZBarImageScanner *scanner = reader.scanner; [scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to:0]; [self presentModalViewController: reader animated:YES]; } - (void) imagePickerController: (UIImagePickerController*) picker didFinishPickingMediaWithInfo: (NSDictionary*) info { UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage]; id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults]; ZBarSymbol *symbol = nil; for(symbol in results) { break; } if(!symbol || !image) { return; } NSLog(@"symbol.data = %@", symbol.data); //最关键的移行代码 self.resultLabel.text = symbol.data; [picker dismissModalViewControllerAnimated: YES]; } 6.给.h添加<UINavigationControllerDelegate,UIImagePickerControllerDelegate> 辅助: zbarTest02是自己研究的源代码.
自己写的demo 记得一定要添加“ZBarSDK文件下的 libzbar.a”
#import <UIKit/UIKit.h> #import "ZBarSDK.h" @interface ViewController : UIViewController<ZBarReaderDelegate> - (IBAction)click:(id)sender; @end #import "ViewController.h" @interface ViewController () @property(nonatomic,retain)ZBarReaderViewController *reader; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)click:(id)sender { self.reader=[[[ZBarReaderViewController alloc] init] autorelease]; self.reader.readerDelegate=self; ZBarImageScanner *scanner=self.reader.scanner; [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0]; [self presentViewController:self.reader animated:YES completion:nil]; ; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ id<NSFastEnumeration> result=[info objectForKey:ZBarReaderControllerResults]; ZBarSymbol *symbol=nil; for (symbol in result) { break; [self.reader dismissViewControllerAnimated:YES completion:nil]; } } @end