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

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.