您好,登錄后才能下訂單哦!
iOS讀取制作二維碼
代碼例子下載地址 "讀取制作二維碼.zip"
http://vdisk.weibo.com/s/Gbaa1
工程中需要的第三方庫在代碼例子中有的
我們來看看.h文件:
#import <UIKit/UIKit.h> #import "ZBarSDK.h" @interface ViewController : UIViewController<ZBarReaderDelegate,UIAlertViewDelegate> @property (strong , nonatomic) UIImageView* qRImageView; @property (strong , nonatomic) ZBarReaderViewController* reader; @property (strong , nonatomic) NSString* qRUrl; -(IBAction)QRPress:(id)sender; -(IBAction)qrBtnPress:(id)sender; @end
.m文件如下:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.qRUrl = [[NSString alloc] init]; //從這以下到32行 self.reader = [[ZBarReaderViewController alloc] init]; self.reader.readerDelegate = self; self.reader.supportedOrientationsMask = ZBarOrientationMaskAll; ZBarImageScanner *scanner = self.reader.scanner; [scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0]; //這部分為zBarSDK文檔提供的例子給出的主要的意思就是初始化ZBarReaderViewController類的對象,以及設置代理回調方法為- (void) p_w_picpathPickerController: (UIImagePickerController*) reader //didFinishPickingMediaWithInfo: (NSDictionary*) info self.qRImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)]; [self.view addSubview:self.qRImageView]; }
//制作二維碼 -(IBAction)qrBtnPress:(id)sender { /*字符轉二維碼 導入 libqrencode文件 引入頭文件#import "QRCodeGenerator.h" 即可使用 */ self.qRImageView.p_w_picpath = [QRCodeGenerator qrImageForString:@"asd戶外" p_w_picpathSize:self.qRImageView.bounds.size.width]; }
//點擊Button時將掃描二維碼需要的照相機視圖,即self.reader(ZBarReaderViewController)類的對象 -(IBAction)QRPress:(id)sender { [self presentViewController:self.reader animated:YES completion:^{ NSLog(@"fd"); }]; }
//這個回調方法的寫法是固定,只要知道以下幾點就好需要的數據是從symbol.data中取出的,因此也就需要在.h文件中聲明一個字符串接受就好。在這里我在.h中聲明了self.qRUrl對象進行接受,之后只要按下面的格式寫就好。 - (void) p_w_picpathPickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info { id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults]; ZBarSymbol *symbol = nil; for(symbol in results) { NSLog(@"symbol =%@",symbol.data); break; } self.qRUrl = symbol.data; UIAlertView* alert = [[UIAlertView alloc] initWithTitle:self.qRUrl message:self.qRUrl delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; self.qRImageView.p_w_picpath = [info objectForKey: UIImagePickerControllerOriginalImage]; [self.reader dismissViewControllerAnimated:YES completion:^{ }]; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。