在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
http://www.lxweimin.com/p/fe466f42aa94