IOS使用ZXing添加二维码功能
在ios7以后,苹果集成了二维码功能,我本人也更加倾向于使用苹果原生的二维码扫描功能,但是老大非要用zxing这个库进行二维码功能的实现,所以就只能去找相关内容咯。
ZXing库下载地址:https://github.com/TheLevelUp/ZXingObjC
虽然内容很大,但是用到的一般只有ZXingObjc文件夹,也就是那个2M多一点的文件夹,感觉下了132M好亏的样子。
导入ZXing库后一般都会报错,因为缺少了一些framework文件(系统的.framework是动态库,我们自己建立的.framework是静态库),需要添加这八个framework文件:
AVFoundation.framework;CoreGraphics.framework;CoreVideo.framework;CoreMedia.framework;QuartzCore.framework;ImageIO.framework;Foundation.framework;UIKit.framework
按照教程所说添加好之后就可以直接用了,但是我添加完以后还是会报错,这就很尴尬,报一些很奇怪的错,像什么NSString未定义之类的。遇到这种情况我是通过添加一个.pch文件解决的。
添加.pch文件的步骤:
- 新建PrefixHeader.pch文件:New File >> Other >> PCH File ;
2.配置PrefixHeader.pch文件路径:项目Targets >> Bulid Setting >> Apple LLVM 8.0 Language >> Prefix Header ; - 修改Precompile Prefix Header为YES:
这样预编译后的PCH文件会被缓存起来,可以提高编译速度;
.pch文件内容(Example是pch文件的名称):
#ifndef Example_pch
#define Example_pch
// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif
#endif /* Example_pch */
上面就是.pch文件的内容了,其实要添加的只有#import <Foundation/Foundation.h>这一行。
然后就是怎么用zxing库的问题了:
生成二维码:
NSString *data = @"https://www.baidu.com";
if (data == 0) return;
ZXMultiFormatWriter *writer = [[ZXMultiFormatWriter alloc] init];
ZXBitMatrix *result = [writer encode:data
format:kBarcodeFormatQRCode
width:self.imageView.frame.size.width
height:self.imageView.frame.size.width
error:nil];
if (result) {
ZXImage *image = [ZXImage imageWithMatrix:result];
self.imageView.image = [UIImage imageWithCGImage:image.cgimage];
} else {
self.imageView.image = nil;
}
扫描二维码会复杂一点:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.capture = [[ZXCapture alloc] init];
self.capture.camera = self.capture.back;
self.capture.focusMode = AVCaptureFocusModeContinuousAutoFocus;
self.capture.rotation = 90.0f;
self.capture.layer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
self.capture.scanRect = CGRectMake(self.view.bounds.size.width/2 -150, self.view.bounds.size.height/2 -150, 300, 300);
[self.view.layer addSublayer:self.capture.layer];
}
- (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result {
if (!result ||self.scanResult) {
return;
}
[self.capture stop];
NSLog(@"扫描结果为:%@\n",result.text);
//如果扫到的是二维码:
if (result.barcodeFormat == kBarcodeFormatQRCode) {
self.scanResult = YES;
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
WebViewController *web = [[WebViewController alloc]init];
AppDelegate *app = [UIApplication sharedApplication].delegate;
NSString *address = [NSString stringWithFormat:@"%@?%@",result,app.remoteManager.token];
web.address = address;
[self.navigationController pushViewController:web animated:YES];
}else{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self.capture start];
});
}
}
这样就可以了,会自动调用摄像头进行扫描,扫到的内容会显示为result,这里我跳转的位置是一个webview,用于打开扫描网址。
本文参考SwordDevil的文章和尘缘驿站的文章;
https://blog.csdn.net/lxmy2012/article/details/53976228
https://www.jianshu.com/p/fe466f42aa94