此前在 iOS 項目中接入過 ZXing 的掃碼能力,這裡把集成過程與使用方式整理出來,供需要的人參考。
從 Google Code 或 GitHub 下載 ZXing,整個倉庫相當龐大,我們只需要 iOS 相關部分。原則上保留 cpp 與 iphone 兩個目錄,其餘全部刪除。
對 cpp 目錄,只保留 cpp/core/src/zxing 下的內容,其餘也可刪去,但目錄結構必須保持原樣,否則編譯會大量報錯。裁剪後的結構如下:
把裁剪後的 zxing 目錄移動到 iOS 項目目錄下,並將 ZXingWidget.xcodeproj 拖進我們的工程。
在 iOS 項目的 Build Phases 中,添加 Target Dependencies 與 Link Binary With Libraries,並引入以下庫:
./zxing/iphone/ZXingWidget/Classes ./zxing/cpp/core/src
注意第一個路徑需勾選遞歸查找子目錄,第二個則不勾選。
ZXing 已經封裝好一個掃碼視圖控制器,接入最快。先在文件中引入頭文件:
#import <ZXingWidgetController.h> #import <QRCodeReader.h>
需要掃描時調用:
- (IBAction)scanPressed:(id)sender
{
ZXingWidgetController *widController =
[[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO];
NSMutableSet *readers = [[NSMutableSet alloc] init];
QRCodeReader *qrcodeReader = [[QRCodeReader alloc] init];
[readers addObject:qrcodeReader];
[qrcodeReader release];
widController.readers = readers;
[readers release];
[self presentModalViewController:widController animated:YES];
[widController release];
}
掃描結果通過 delegate 回落:
@protocol ZXingDelegate - (void)zxingController:(ZXingWidgetController *)controller didScanResult:(NSString *)result; - (void)zxingControllerDidCancel:(ZXingWidgetController *)controller; @end
方法二與方法一的關係,就像 AVCaptureSession 與 UIImagePickerController 的關係:前者更麻煩,但可定制性高得多。做法是自行用 AVFoundation 取得相機實時畫面,轉成 UIImage,再交給 ZXing 的 Decoder 解碼。
#import "Decoder.h"
#import "TwoDDecoderResult.h"
#import "QRCodeReader.h"
- (void)viewDidLoad
{
// 準備 QR 讀取器集合
self.qrReader = [[NSMutableSet alloc] init];
QRCodeReader *qrcodeReader = [[QRCodeReader alloc] init];
[self.qrReader addObject:qrcodeReader];
self.scanningQR = NO;
self.step = STEP_QR;
}
// AVFoundation 採樣回調
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
// 第一步:把 sampleBuffer 轉成 UIImage
UIImage *image = [self getCaptureImage:sampleBuffer];
// 第二步:交給 Decoder 識別
Decoder *d = [[Decoder alloc] init];
d.readers = self.qrReader;
d.delegate = self;
self.scanningQR = [d decodeImage:image] == YES ? NO : YES;
}
Decoder 提供的回調協議:
@protocol DecoderDelegate <NSObject>
@optional
- (void)decoder:(Decoder *)decoder willDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset;
- (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset
withResult:(TwoDDecoderResult *)result {
NSLog(@"result = %@", [result text]);
}
- (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset
reason:(NSString *)reason;
- (void)decoder:(Decoder *)decoder foundPossibleResultPoint:(CGPoint)point;
@end
接入過程中最麻煩的是編譯問題:
.m 改為 .mm,因為 ZXing 核心是 C++ 實現。Build Active Architecture Only 改為 NO。ZXing 2.1 與 OpenCV 2.4.3 的 iOS 版本存在兼容衝突,二者對 C++ 標準庫版本與編譯器版本的要求不一致,滿足一方另一方就編譯失敗。社區裡找到的解法可讓它們共存,但僅適用於 iOS 5.0 及以上。若你也遇到同樣問題,可參考相關討論帖。
需要說明的是,ZXing 的 Objective-C 分支(ZXingObjC)與 iOS 原生 AVCaptureMetadataOutput 都已能輕鬆完成二維碼識別。若只需掃描常見碼制,直接使用原生 API 幾行代碼即可搞定,無需引入這麼重的庫:
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init]; [session addOutput:output]; [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
權衡點在於:ZXing 支持的碼制更全、可定制性更強,但集成成本高;原生方案集成簡單、性能更好,但對 PDF417、Aztec 等冷門格式的支持有限。