天天看点

开博来的第一篇文贴:zxing2.0二维码在xcode4.2中的使用

虽然网上有很多关于zxing的使用方法,不过查了很多中文和英文的贴子。发现说的不够详细,差一步就错了很多!

在这里写下刚做的心得(注:这里只针对iphone版本,xcode4.2。SDK是5.1,当然听说最新的zxing要4.0以上才可以)。不知写的好不好。如果有朋友出现按我的做法,没有成功的。欢迎指出!

以下的步骤,就不截图了

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"(在添加前,请先运行该项目,进行编译,如果成功,再进行此步添加!)

,并确保"拷贝到项目文件夹中"不被选中。然后点添加。项目结构如下图:

开博来的第一篇文贴:zxing2.0二维码在xcode4.2中的使用

    3.4:选择你的项目--TARGETS--Build Phases---Target Dependencies----然后点击"+"添加“ZXingWidget”。添加后如下图:

开博来的第一篇文贴:zxing2.0二维码在xcode4.2中的使用

3.5:同样,添加frameWorks.方法:Build Phases---Target Dependencies----”Link Binary With Libraries”---点击"+"。添加如下几项:

libZXingWidget.a

AddressBook

AddressBookUI

AudioToolbox

AVFoundation

CoreMedia

CoreVideo

libiconv.dylib

完成后如下图:

开博来的第一篇文贴:zxing2.0二维码在xcode4.2中的使用

3.5:按下图做操作,这里很重要。

1. Build Settings --- 2. 搜索"header search paths" --- 3. 双击"Header Search Paths"

4. 添加 路径:如图,注意这里"Recursive"画蓝色线的地方请打勾,另一个不要打勾

开博来的第一篇文贴:zxing2.0二维码在xcode4.2中的使用

3.6:这步操作,最好做一下,因为不知道为产生什么问题

如下图中,"2" 标注处的CoreSrc这个组本来是没有的。所以大家在做的时候,最好手动建一下这个组,然后按"1"标注处那样。添加zxing。当然不需要拷贝了。因为在 "1" 处已经存在过了

开博来的第一篇文贴:zxing2.0二维码在xcode4.2中的使用

4.在项目中引用

这一步会经常出现一些文件找不到等问题

首先请做如下操作:

1.请更改你的delegate文件的.m文件为.mm (请注意,如果你在项目中重命名,并不会使物理路径中的文件名被更改,所以请更改物理文件名)

MyZxingAppDelegate.m 

>>>>

MyZxingAppDelegate.mm

2.请更改你要使用zxing的项目文件的.m文件为.mm

MyZxingViewController.m 

>>>>

MyZxing

ViewController

 .mm

需要注意的是,如果不做更改,可能会引发:XXX file not found的问题。最好是main.m也更改一下

5. 

MyZxingViewController 文件夹中引用

.h

[cpp]  view plain copy print ?

  1. //  
  2. //  Copyright 2011 __MyCompanyName__. All rights reserved.  
  3. //  
  4. #import <UIKit/UIKit.h>  
  5. #import "ZXingWidgetController.h"  
  6. //#import "QRCodeReader.h"  //这个引用在.h文件中为出错:<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; ">iostream file not found</span>  
  7. @interface Contact : UIViewController <ZXingDelegate>{  
  8.     UITextView *resultsView;  
  9.     NSString *resultsToDisplay;  
  10. }  
  11. @property (retain, nonatomic) IBOutlet UITextView *resultsView;  
  12. @property (nonatomic, copy) NSString *resultsToDisplay;  
  13. - (IBAction)scanPressed:(id)sender;  
  14. @end  

.mm

[cpp]  view plain copy print ?

  1. //  
  2. //  Created by yangke nicky on 11-12-30.  
  3. //  Copyright 2011 __MyCompanyName__. All rights reserved.  
  4. //  
  5. //.mm中才可以引用  
  6. #import "QRCodeReader.h"  
  7. @implementation Contact  
  8. @synthesize resultsView;  
  9. @synthesize resultsToDisplay;  
  10. //正常扫描退出事件  
  11. - (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result {  
  12.     self.resultsToDisplay = result;  
  13.     if (self.isViewLoaded) {  
  14.         [resultsView setText:resultsToDisplay];  
  15.         [resultsView setNeedsDisplay];  
  16.     }  
  17.     [self dismissModalViewControllerAnimated:NO];  
  18. }  
  19. //扫描界面退出按钮事件  
  20. - (void)zxingControllerDidCancel:(ZXingWidgetController*)controller {  
  21.     [self dismissModalViewControllerAnimated:YES];  
  22. }  
  23. - (void) viewDidLoad  
  24. {  
  25.     [super viewDidLoad];  
  26.     [resultsView setText:resultsToDisplay];  
  27. }  
  28. //扫描按钮事件  
  29. - (IBAction)scanPressed:(id)sender   
  30. {  
  31.     ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO];  
  32.     QRCodeReader *qrcodeReader = [[QRCodeReader alloc] init];  
  33.     NSSet *readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];  
  34.     [qrcodeReader release];  
  35.     widController.readers = readers;  
  36.     [readers release];  
  37.     NSBundle *mainBundle = [NSBundle mainBundle];  
  38.     //aiff  
  39.     widController.soundToPlay =[NSURL fileURLWithPath:[mainBundle pathForResource:@"beep-beep" ofType:@"caf"] isDirectory:NO];  
  40.     [self presentModalViewController:widController animated:YES];  
  41.     [widController release];  
  42. }  
  43. - (void)viewDidUnload {  
  44.     self.resultsView = nil;  
  45. }  
  46. - (void)dealloc {  
  47.     [resultsView release];  
  48.     [resultsToDisplay release];  
  49.     [super dealloc];  
  50. }  
  51. - (void)didReceiveMemoryWarning {  
  52.     // Releases the view if it doesn't have a superview.  
  53.     [super didReceiveMemoryWarning];  
  54.     // Release any cached data, images, etc that aren't in use.  
  55. }  
  56. @end  

教程基本写完了,希望对大家有用处。接下来,我要写联系人二维码保存到联系人中。目前正在进行中,等写完了,再发表出来!

之前,推送问题,也搞定了。抽空发上来!