ZXing(Github镜像地址)是一个开源的条码生成和扫描库(开源协议为Apache2.0)。它不但支持众多的条码格式,而且有各种语言的实现版本,它支持的语言包括:Java, C++, C#, Objective-C, ActionScript和Ruby。
二:
5.我们需要设置ZXing项目和我们原本的iOS项目之间的依赖关系。在我们的iOS项目的设置中,点击build phases tab,然后增加 Target Dependencies 和 Link binary,并且增加这些framework依赖:
完成之后如下图:
二:
最后一步,在设置中增加如下2个header search path:
./zxing/iphone/ZXingWidget/Classes
./zxing/cpp/core/src
需要注意的是,第一个path要设置成循环查找子目录,而第二个不循环查找,如下图所示:
注意:我在使用中遇到了一些问题,主要是编译的问题。
1.一个是找不到 头文件。解决方法:把用到ZXing的源文件扩展名由.m改成.mm。
2.报错:Undefined symbols for architecture armv7s,解决方法:把ZXingWidget的一个build target参数:”Build Active Architecture Only” 修改成 “NO”.
3.报错:No such file or directory,出现该错误可能是你的Header Search Path写错了,或者就是你的zxing库的目录结构不是我上面强调的,好好检查一下吧。
4.如果遇到下面问题:
std::allocator<char> const&)", referenced from: (null): "std::string::append(std::string const&)", referenced from: (null): "std::__throw_out_of_range(char const*)", referenced from: (null): "std::ios_base::Init::Init()", referenced from: (null): "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&)", referenced from: (null): "std::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_ostringstream()", referenced from: (null): "std::string::append(unsigned long, char)", referenced from: (null): "std::string::_M_leak_hard()", referenced from: (null): "std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::str() const", referenced from: (null): "std::string::erase(unsigned long, unsigned long)", referenced from: (null): Linker command failed with exit code 1 (use -v to see invocation)
我们设置一下:
这样就ok了。
在工程中的用法是:
(void)pressButton1:(UIButton *)button { ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:YES]; NSMutableSet *readers = [[NSMutableSet alloc] init]; QRCodeReader *qrcodeReader = [[QRCodeReader alloc] init]; MultiFormatOneDReader *OneReaders=[[MultiFormatOneDReader alloc]init]; [readers addObject:qrcodeReader]; [readers addObject:OneReaders]; widController.readers = readers; [self presentViewController:widController animated:YES completion:^{}]; }