iOS之使用ZbarSDK实现扫描二维码以及条形码功能
Posted 学东哥哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS之使用ZbarSDK实现扫描二维码以及条形码功能相关的知识,希望对你有一定的参考价值。
1、去下载ZbarSDK。
2、新建你的ios Project。
3、导入ZbarSDK,直接拖动你下载的ZbarSDK到你的项目,并且选择copy选项。
4、添加如下framework
5、在appDelegate文件的以下方法中加入如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //your code [ZBarReaderView class]; return YES; }
6、在你需要使用扫描功能的ViewController.h文件添加如下:
#import <UIKit/UIKit.h> #import "ZBarSDK.h" @interface ScanViewController : UIViewController<ZBarReaderViewDelegate> { ZBarReaderView *readerView; ZBarCameraSimulator *cameraSim; } @property (retain, nonatomic) IBOutlet ZBarReaderView *readerView; @end
7、在你需要使用扫描功能的ViewController.m文件添加如下:
#import "ScanViewController.h" @interface ScanViewController () @end @implementation ScanViewController @synthesize readerView; - (void)viewDidLoad { [super viewDidLoad]; //your code readerView.readerDelegate = self; [readerView setAllowsPinchZoom:YES]; if (TARGET_IPHONE_SIMULATOR) { cameraSim = [[ZBarCameraSimulator alloc] initWithViewController:self]; cameraSim.readerView = readerView; } } -(void)viewDidAppear:(BOOL)animated { [readerView start]; } -(void)viewDidDisappear:(BOOL)animated { [readerView stop]; } -(void) readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image { NSString *codeData = [[NSString alloc] init];; for (ZBarSymbol *sym in symbols) { codeData = sym.data; break; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"掃描結果" message:codeData delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // 得到条形码结果 id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults]; ZBarSymbol *symbol = nil; for(symbol in results) break; //获得到条形码 //NSString *dataNum=symbol.data; //扫描界面退出 [picker dismissModalViewControllerAnimated: YES]; }
以上是关于iOS之使用ZbarSDK实现扫描二维码以及条形码功能的主要内容,如果未能解决你的问题,请参考以下文章