虽然网上有很多关于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 这是我写博客时的版本,具体版本请参考网站 |
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.方法:Build Phases---Target Dependencies----”Link Binary With Libraries”---点击"+"。添加如下几项:
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 file not found的问题。最好是main.m也更改一下
MyZxingViewController 文件夹中引用
.h
//
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import
#import "ZXingWidgetController.h"
//#import "QRCodeReader.h" //这个引用在.h文件中为出错:iostream file not found
@interface Contact : UIViewController {
UITextView *resultsView;
NSString *resultsToDisplay;
}
@property (retain, nonatomic) IBOutlet UITextView *resultsView;
@property (nonatomic, copy) NSString *resultsToDisplay;
- (IBAction)scanPressed:(id)sender;
@end
.mm
//
// Created by yangke nicky on 11-12-30.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
//.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];
}
- (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 {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
@end
之前,推送问题,也搞定了。抽空发上来!
如有问题,大家可以加入 QQ群:28433020 交流