1.准备工作(下载库文件)
1.1下载zxing最新的库,下载地址
它支持众多的条码格式(对于IOS的只能支持二维码,ZXING相对于JAVA支持较好),而且有各种语言的实现版本,它支持的语言包括:Java, C++, C#, Objective-C, ActionScript和Ruby。不是所有的文件我们都用的到,下载zxing后,解压得到zxing文件夹,里面只保留 cpp 和 iphone 两个文件夹就可以了。(裁剪好后,请先运行该项目,进行编译,如果成功,再进行下一步骤的添加工作)
3.2:打开你的项目,新建GROUP命名为"Dependencies"。
3.3:右击"Dependencies"选择“Add files to...”,在弹出的框中,找到:你的项目文件夹/zxing/iphone/ZXingWidget下面,选择"ZXingWidget.xcodeproj",并确保"拷贝到项目文件夹中"不被选中。这样的话,只是对其的一个引用,然后点添加。项目结构如下图:
3.4:建立自己的项目同ZXING的依赖关系
选择你的项目--TARGETS--Build Phases--Target Dependencies--然后点击"+"添加“ZXingWidget”。添加后如下图:
3.5:添加相关的frameWorks类库
选择你的项目--TARGETS--Build Phases--Link Binary With Library--然后点击"+"添加以下frameWorks:
libZXingWidget.a
AddressBook
AddressBookUI
AudioToolbox
AVFoundation
CoreMedia
CoreVideo
libiconv.dylib
完成后如下图:
3.5:设置 搜索头文件
选择你的项目--TARGETS--Build Settings--搜索"header search paths"--双击"Header Search Paths"
./zxing/iphone/ZXingWidget/Classes
至此ZXING的环境集成完成,接下来是向项目中引用ZXING
----------------------------------------------------------------------
4.在项目中引用ZXING文件
这一步会经常出现一些文件找不到等问题
首先请做如下操作:
4.1 由于混编,需要改部分文件的.m为.mm,需要注意的是,如果不做更改,可能会引发:XXX file not found的问题。
1.请更改你的delegate文件的.m文件为.mm (请注意,如果你在项目中重命名,并不会使物理路径中的文件名被更改,所以请更改物理文件名)
MyZxingAppDelegate.m
>>>>
MyZxingViewController.m
>>>>
MyZxing
ViewController
.mmMyZxingViewController 文件夹中引用
- 某文件.h
- #import <UIKit/UIKit.h>
- #import <ZXingWidgetController.h> //注意是<> (" <> "代表是库文件 " “” "代表自己写的文件)
- @interface Contact : UIViewController <ZXingDelegate>{
- UITextView *resultsView;
- NSString *resultsToDisplay;
- }
- @property (retain, nonatomic) IBOutlet UITextView *resultsView;
- @property (nonatomic, copy) NSString *resultsToDisplay;
- - (IBAction)scanPressed:(id)sender;
- @end
.mm
- 某文件.mm
- //.mm中才可以引用
- #import <QRCodeReader.h> //这个引用在.h文件中会出错:应该在.mm文件里引入,否则会有“iostream file not found”的错误
- @implementation Contact
- @synthesize resultsView;
- @synthesize resultsToDisplay;
- //正常扫描退出事件
- - (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result {
- self.resultsToDisplay = result;
- if (self.isViewLoaded) {
- [resultsView setText:resultsToDisplay];
- [resultsView setNeedsDisplay];
- }
- [self dismissModalViewControllerAnimated:NO];
- }
- //扫描界面退出按钮事件
- - (void)zxingControllerDidCancel:(ZXingWidgetController*)controller {
- [self dismissModalViewControllerAnimated:YES];
- }
- - (void) viewDidLoad
- {
- [super viewDidLoad];
- [resultsView setText:resultsToDisplay];
- }
- //扫描按钮事件
- - (IBAction)scanPressed:(id)sender
- {
- ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO];
- QRCodeReader *qrcodeReader = [[QRCodeReader alloc] init];
- NSSet *readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];
- [qrcodeReader release];
- widController.readers = readers;
- [readers release];
- NSBundle *mainBundle = [NSBundle mainBundle];
- //aiff
- widController.soundToPlay =[NSURL fileURLWithPath:[mainBundle pathForResource:@"beep-beep" ofType:@"caf"] isDirectory:NO];
- [self presentModalViewController:widController animated:YES];
- [widController release];
- }
- - (void)viewDidUnload {
- self.resultsView = nil;
- }
- - (void)dealloc {
- [resultsView release];
- [resultsToDisplay release];
- [super dealloc];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- @end