標籤:
1. 從 http://zbar.sourceforge.net/iphone 下載最新版本的ZBar SDK。
2. 雙擊下載好的檔案。目前版本是:ZBarSDK-1.2.dmg。
3. 按提示將ZBarSDK拖拽到我們自己的項目中。
4. 使用ZBarSDK,還需要匯入其他的framework。
工程 -> target -> build phases -> Link Binary With Libraries -> 點擊+添加以下framework。
1>.AVFoundation.framework
2>.CoreMedia.framework
3>.CoreVideo.framework
4>.QuartzCore.framework
5>.libiconv.dylib
5. 匯入標頭檔:#import "ZBarSDK.h"
6. 聲明支援代理協議:<ZBarReaderDelegate>
7. 書寫代碼:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title = @"二維碼掃描"; UIImageView * imageView = [[UIImageView alloc] init]; [imageView setFrame:CGRectMake(300, 162, 424, 424)]; [self.view addSubview:imageView]; self.resultImage = imageView; [self scan];}- (void)scan{
// ZBarReaderViewController * reader = [[ZBarReaderViewController alloc] init]; reader.readerDelegate = self; reader.supportedOrientationsMask = ZBarOrientationMaskAll; ZBarImageScanner * scanner = reader.scanner; [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0]; [self presentViewController:reader animated:YES completion:nil];}- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
// 得到條碼結果
id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults]; ZBarSymbol * symbol = nil; for (symbol in results) { break; }
// 列印條碼
self.resultImage.image = [info objectForKey:UIImagePickerControllerOriginalImage]; [picker dismissViewControllerAnimated:YES completion:nil];
// 退出掃描介面
MyLog(@"result:%@", symbol.data);}
使用ZbarSDK實現掃描二維碼以及條碼功能(iOS)