最近公司接了一個項目,是海爾公司的,裏面有二維碼掃描的功能,開源的二維碼掃描的sdk 有不少,今天我這裏使用的ZBarSDK,一個比較優秀的開源項目,使用起來也很簡單。 html
ZBarSDK是一個開源的SDK,可從這裏下載到源碼,該SDK實現了識別和讀取各類條形碼,包括EAN-13/UPC-A, UPC-E, EAN-8, Code 128, Code 39, Interleaved 2 of 5 和 QR Code。 web
幫助文檔:http://zbar.sourceforge.net/iphone/sdkdoc/index.html iphone
Step1.使用ZBarSDK 須要導入的framework ide
1.AVFoundation.framework 佈局
2.CoreMedia.framework atom
3.CoreVideo.framework spa
4.QuartzCore.framework .net
5.libiconv.dylib code
Step2.在ViewController.h 導入#import "ZBarSDK.h" htm
Step3.在ViewController.h 繼承 <ZBarReaderDelegate>協議
Step4.寫代碼:
ViewController.h
#import <UIKit/UIKit.h> #import "ZBarSDK.h" @interface ViewController : UIViewController <ZBarReaderDelegate> @property (weak, nonatomic) IBOutlet UIImageView *imageView; - (IBAction)scan:(id)sender; @property (weak, nonatomic) IBOutlet UILabel *label; @endViewController.m
#import "ViewController.h" #import "ZBarSDK.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)scan:(id)sender { ZBarReaderViewController * reader = [ZBarReaderViewController new]; reader.readerDelegate = self; ZBarImageScanner * scanner = reader.scanner; [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0]; reader.showsZBarControls = YES; [self presentViewController:reader animated:YES completion:nil]; } -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults]; ZBarSymbol * symbol; for(symbol in results) break; _imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage]; [picker dismissViewControllerAnimated:YES completion:nil]; _label.text = symbol.data; } @end在界面上放置一個UIImageView/UILabel/UIButton, UIImageView 用於顯示掃描到的圖像,UILabel用於顯示掃描到的信息,UIButton 用於響應事件。
佈局如圖: