iOS7,AVFoundation中現在已經內置支持一維和二維碼的掃瞄,iOS6及之前的想要掃瞄二維碼,還是需要添加第三方庫ZXing和ZBar。爲了統一開發,兼容所有版本,我們可以只使用ZBar開源框架,但是用最新SDK,會報錯,我們必須先解決arm64,arm7s兼容和中文亂碼問題。
ZBar官網:點擊打開鏈接
一、解決arm64,arm7s兼容並解決中文亂碼問題
解決這個問題,需要替換libzbar.a,這裏就不給出方法,直接給出下載地址:點擊打開鏈接。
下載完成之後,刪除原來SDK包裏的libzbar.a,換成和這個就好了。
二、導入ZBarSDK需要的framework
AVFoundation.framework
CoreMedia.framework
CoreVideo.framework
QuartzCore.framework
libiconv.dylib
三、代碼編寫:
ViewController.h 中導入#import "ZBarSDK.h"並繼承 <ZBarReaderDelegate>協議
關鍵代碼:
- (void) scan{
ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
ZBarImageScanner *scanner = reader.scanner;
// TODO: (optional) additional reader configuration here
// EXAMPLE: disable rarely used I2/5 to improve performance
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
// present and release the controller
[self presentModalViewController: reader
animated: YES];
}
- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
{
// ADD: get the decode results
id<NSFastEnumeration> results =
[info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)
// EXAMPLE: just grab the first barcode
break;
// EXAMPLE: do something useful with the barcode data
resultText.text = symbol.data;
// EXAMPLE: do something useful with the barcode image
resultImage.image =
[info objectForKey: UIImagePickerControllerOriginalImage];
// ADD: dismiss the controller (NB dismiss from the *reader*!)
[reader dismissModalViewControllerAnimated: YES];
}