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