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]; }
时间: 2024-09-29 22:07:36