天天看點

Zxing使用及常見錯誤(iOS)

1.準備工作(以下的步驟将以zxing2.0為基礎)

1.1下載下傳zxing最新的庫,位址:http://code.google.com/p/zxing/downloads/list (如有變動,此文博不給于更新)。找到

ZXing-2.0.zip

ZXing 2.0 Release 這是我寫部落格時的版本,具體版本請參考網站

1.2 再次聲明一下,不要用模拟器去做測試了。既然是二維碼的使用,那就會用到攝像頭,是以要用真機來測試

2.關于庫裡的一些檔案

不是所有的檔案我們都用的到,

下載下傳zxing2.0後,解壓得到zxing-2.0檔案夾,裡面隻保留 cpp 和 iphone 兩個檔案夾就可以了。

3.zxing2.0導入到你的項目中

3.1 :拷貝zxing-2.0到你的項目根檔案夾下面

3.2:打開你的項目,建立GROUP命名為"Dependencies"。

3.3:右擊"Dependencies"選擇“Add files to...”,在彈出的框中,找到:你的項目檔案夾/zxing-2.0/iphone/ZXingWidget下面,選擇"ZXingWidget.xcodeproj"(在添加前,請先運作該項目,進行編譯,如果成功,再進行此步添加!)

,并確定"拷貝到項目檔案夾中"不被選中。然後點添加。項目結構如下圖:

3.4:選擇你的項目--TARGETS--Build Phases---Target Dependencies----然後點選"+"添加“ZXingWidget”。添加後如下圖:

3.5:同樣,添加frameWorks.方法:BuildPhases---Target Dependencies----”LinkBinary WithLibraries”---點選"+"。添加如下幾項:

libZXingWidget.a

AddressBook

AddressBookUI

AudioToolbox

AVFoundation

CoreMedia

CoreVideo

libiconv.dylib

完成後如下圖:

3.5:按下圖做操作,這裡很重要。

1. Build Settings --- 2. 搜尋"header search paths" --- 3. 輕按兩下"Header Search Paths"

4. 添加 路徑:如圖,注意這裡"Recursive"畫藍色線的地方請打勾,另一個不要打勾

3.6:這步操作,最好做一下,因為不知道為産生什麼問題

如下圖中,"2" 标注處的CoreSrc這個組本來是沒有的。是以大家在做的時候,最好手動建一下這個組,然後按"1"标注處那樣。添加zxing。當然不需要拷貝了。因為在 "1" 處已經存在過了

4.在項目中引用

這一步會經常出現一些檔案找不到等問題

首先請做如下操作:

1.請更改你的delegate檔案的.m檔案為.mm(請注意,如果你在項目中重命名,并不會使實體路徑中的檔案名被更改,是以請更改實體檔案名)

MyZxingAppDelegate.m

>>>>

MyZxingAppDelegate.mm

2.請更改你要使用zxing的項目檔案的.m檔案為.mm

MyZxingViewController.m

MyZxing

ViewController

.mm

需要注意的是,如果不做更改,可能會引發:XXX filenot found的問題。最好是main.m也更改一下

MyZxingViewController 檔案夾中引用

.h

使用zxing常見問題

1.使用zxing的方法參考文檔http://blog.csdn.net/icash/article/details/7727299。

2.如果你項目中使用了zxing,但是在xcdoe更新到4.5以後出現Incompatible pointer types sending 'Class' (aka 'Class *') to parameter of type 'id<NSCopying>'錯誤,解決辦法:重新下載下傳更新版本的ZXing, 可以使用指令下載下傳最新版本:svn checkout http://zxing.googlecode.com/svn/trunk/ zxing-read-only。

3.如果出現了問題Undefined symbols for architecture i386"std::string::c_str() const", referenced from。

将你項目中的Apple LLVM compiler 4.1 - language中的 c Language Dialect、c++ Language Dialect、c++ standard Library設定成下圖所選的值。參考http://stackoverflow.com/questions/12665457/zxing-in-xcode-4-5-and-ios-6

Zxing使用及常見錯誤(iOS)

4.編譯成功往真機上裝的時候出現Choose a destination with a supported architecture in order to run on this device ,由于ios裝置不支援armv7s,是以必須将Architectures設定為armv6,但是僅僅需要改動valid architectures就行,不要改動architectures,否則容易引起真機不運作。把architectures改為$(ARCHS_STANDARD_32_BIT)就可以撞到手機上了。見http://blog.sina.com.cn/s/blog_90a0ad8d01013uuh.html

分享到:

5 SenTestingKit/SenTestingKit.h: No such file or directory錯誤

問題的原因,與單元測試有關的源檔案添加到了不正确的Targets中,單元測試的Target與另外的主程式檔案是屬于不同的target的。

兩種解決辦法:

1:重新添加.h和.m檔案,在彈出的對話框的底部有一個“Add to targets”選項,一定要選中那個與單元測試相對應的xxxTests項,重新編譯。

2:找到Target的Build Phases設定,仔細檢查Compile Sources裡的檔案清單,把與單元測試有關的.m檔案從主程式清單中移除,加到xxxTests這個Target中,重新編譯。

6

#import <UIKit/UIKit.h>

#import "ZXingWidgetController.h"

//#import "QRCodeReader.h"//這個引用在.h檔案中為出錯:iostream file notfound

@interface Contact : UIViewController <ZXingDelegate>{

   UITextView *resultsView;

   NSString *resultsToDisplay;

}

@property (retain, nonatomic) IBOutlet UITextView *resultsView;

@property (nonatomic, copy) NSString *resultsToDisplay;

- (IBAction)scanPressed:(id)sender;

@end

<col>

//.mm中才可以引用  

#import "QRCodeReader.h"  

@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];  

{  

   [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 {  

   // Releases the view if it doesn't have a superview.  

   [super didReceiveMemoryWarning];  

   // Release any cached data, p_w_picpaths, etc that aren't in use.