本文實(shí)例為大家分享了iOS第三方二維碼生成與掃描的具體代碼,供大家參考,具體內(nèi)容如下
導(dǎo)入第三方框架libqrencode(生成二維碼)和ZBarSDK(二維碼掃描)
導(dǎo)入頭文件
#import "QRCodeGenerator.h"#import "ZBarSDK.h"
創(chuàng)建兩個(gè)按鈕點(diǎn)擊事件
點(diǎn)擊按鈕一實(shí)現(xiàn)生成二維碼
- (IBAction)erWeiMaBtn1:(id)sender {// 設(shè)置圖片框存放二維碼 UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 200)/2, 100, 200, 200)];// 展示到主視圖 [self.view addSubview:self.imgView];// 利用第三方方法實(shí)現(xiàn)自己想要的二維碼 imgView.image = [QRCodeGenerator qrImageForString:@"傻子" imageSize:self.imgView.frame.size.width];}
點(diǎn)擊按鈕二實(shí)現(xiàn)掃描二維碼
- (IBAction)erWeiMaBtn1:(id)sender { //設(shè)置文本域(設(shè)置為全局) _textView = [[UITextView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-300)/2, 430, 300, 300) textContainer:nil]; //設(shè)置背景顏色 _textView.backgroundColor = [UIColor blueColor]; //添加到主視圖 [self.view addSubview:self.textView]; ZBarReaderViewController *reader = [[ZBarReaderViewController alloc] init]; reader.readerDelegate =self; //隱藏底部控制按鈕 reader.showsZBarControls =YES; //設(shè)置自己定義的界面 //reader.cameraOverlayView = someView;(這個(gè)視圖可以自己定義,當(dāng)然你也可以通過[reader.view addSubview:someView]的方式添加蒙版視圖,這樣可以做出自己想要的視圖效果) //設(shè)置識(shí)別范圍(這里的4個(gè)參數(shù)都是視圖比例~~不要搞錯(cuò)奧(距離左邊的比例,距離上邊的比例,識(shí)別寬度的比例,識(shí)別高度的比例)) //reader.scanCrop =CGRectMake(0,0, 1,1); //設(shè)置識(shí)別的參數(shù) ZBarImageScanner *scanner = reader.scanner; [scanner setSymbology:ZBAR_I25 //這個(gè)參數(shù)和to:后面的參數(shù)配合確定了識(shí)別的編碼范圍,根據(jù)需求調(diào)整,可以提高識(shí)別速度。 config:ZBAR_CFG_ENABLE to:0]; [self presentViewController:reader animated:YES completion:nil];}//下面是委托方法- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info{ id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults]; ZBarSymbol *symbol =nil; for(symbol in results) break; NSLog(@"%@",symbol.data);//打印識(shí)別的數(shù)據(jù) //設(shè)置內(nèi)容 _textView.text = symbol.data; // [reader dismissModalViewControllerAnimated:YES];}
注意:在info.plist中填入可用相機(jī)掃描的白名單!
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注