使用ZBar来读取条形码和二维码的方法
1.使用ZBar项目。下载地址是:http://zbar.sourceforge.net/iphone/index.html
2.新建一个项目。
3.导入 ZBar的sdk。把ZBar SDK的目录拉入项目,然后选中copy选项
4.在项目文件的target中加入 以下framework
5.在appDelegate文件中加入 标记部分的代码
- -(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions
- {
- self.window=[[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];
- //Overridepointforcustomizationafterapplicationlaunch.
- self.viewController=[[[ViewControlleralloc]initWithNibName:@"ViewController"bundle:nil]autorelease];
- self.window.rootViewController=self.viewController;
- [self.windowmakeKeyAndVisible];
- //forceviewclasstoloadsoitmaybereferenceddirectlyfromNIB
- [ZBarReaderViewclass];
- returnYES;
- }
注意此代码:
- //forceviewclasstoloadsoitmaybereferenceddirectlyfromNIB
- [ZBarReaderViewclass];
- //
- //ViewController.h
- //FootSafety
- //
- //Createdby泽宇徐on12-6-12.
- //Copyright(c)2012年__MyCompanyName__.Allrightsreserved.
- //
- #import<UIKit/UIKit.h>
- #import"ZBarSDK.h"
- @interfaceViewController:UIViewController<ZBarReaderViewDelegate>
- {
- IBOutletUILabel*label;
- ZBarReaderView*readerView;
- ZBarCameraSimulator*cameraSim;
- }
- @property(nonatomic,retain)UILabel*label;
- @property(nonatomic,retain)IBOutletZBarReaderView*readerView;
- @end
7.在.m文件中要实现的主要方法是:
- -(void)readerView:(ZBarReaderView*)view
- didReadSymbols:(ZBarSymbolSet*)syms
- fromImage:(UIImage*)img
- {
- //dosomethingusefulwithresults
- for(ZBarSymbol*syminsyms){
- self.label.text=sym.data;
- break;
- }
- }
这里是功能是读取照片信息,把条码放如label显示
- -(void)viewDidAppear:(BOOL)animated
- {
- //runthereaderwhentheviewisvisible
- [readerViewstart];
- }
这个是在显示视图的时候,启动摄像头,开始扫描
- -(void)viewDidLoad
- {
- [superviewDidLoad];
- //Doanyadditionalsetupafterloadingtheview,typicallyfromanib.
- //thedelegatereceivesdecoderesults
- readerView.readerDelegate=self;
- //youcanusethistosupportthesimulator
- if(TARGET_IPHONE_SIMULATOR){
- cameraSim=[[ZBarCameraSimulatoralloc]
- initWithViewController:self];
- cameraSim.readerView=readerView;
- }
- }
在初始化的时候,设置托管。
.m文件所有内容是:
- //
- //ViewController.m
- //FootSafety
- //
- //Createdby泽宇徐on12-6-12.
- //Copyright(c)2012年__MyCompanyName__.Allrightsreserved.
- //
- #import"ViewController.h"
- @interfaceViewController()
- @end
- @implementationViewController
- @synthesizelabel;
- @synthesizereaderView;
- -(void)viewDidLoad
- {
- [superviewDidLoad];
- //Doanyadditionalsetupafterloadingtheview,typicallyfromanib.
- //thedelegatereceivesdecoderesults
- readerView.readerDelegate=self;
- //youcanusethistosupportthesimulator
- if(TARGET_IPHONE_SIMULATOR){
- cameraSim=[[ZBarCameraSimulatoralloc]
- initWithViewController:self];
- cameraSim.readerView=readerView;
- }
- }
- -(void)viewDidAppear:(BOOL)animated
- {
- //runthereaderwhentheviewisvisible
- [readerViewstart];
- }
- -(void)readerView:(ZBarReaderView*)view
- didReadSymbols:(ZBarSymbolSet*)syms
- fromImage:(UIImage*)img
- {
- //dosomethingusefulwithresults
- for(ZBarSymbol*syminsyms){
- self.label.text=sym.data;
- break;
- }
- }
- -(void)viewDidUnload
- {
- [superviewDidUnload];
- //Releaseanyretainedsubviewsofthemainview.
- }
- -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return(interfaceOrientation!=UIInterfaceOrientationPortraitUpsideDown);
- }
- -(void)dealloc
- {
- [self.readerViewrelease];
- [self.labelrelease];
- [superdealloc];
- }
- @end
在ViewController.xib文件中 增加一个view,并且修改view的类是ZBarReaderView ,并且指向 .h文件中定义的
- ZBarReaderView*readerView;