您好,登錄后才能下訂單哦!
最近做了個外包項目,里面用到了二維碼掃描和微信支付!之前比較熟悉的是ZXing,但是在Xcode7.1里面發(fā)現(xiàn)竟然莫名的不支持,木有辦法,從網(wǎng)上查了一下還有一種支持二維碼掃描的東西,沒錯就是接下來我要說的東東,二維碼掃描的利器 ZBarSDK。閑言少絮叨,言歸正傳!
1、下載 ZBarSDK 官網(wǎng) https://github.com/bmorton/ZBarSDK
2、導(dǎo)入如下框架
3、在AppDelegetem文件中#import "ZBarSDK.h"
并且 在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
里面添加下面的代碼
//二維碼讀取
[ZBarReaderViewclass];
4、在二維碼掃描界面開始#import "ZBarSDK.h" ,添加
<ZBarReaderDelegate,ZBarReaderViewDelegate>委托
下面開始介紹掃描步驟:
1.通過攝像頭獲取圖片
2.通過Zbar的內(nèi)部處理方法來識別圖片
3.處理信息并顯示
代碼實現(xiàn):
1.創(chuàng)建視圖
-(void)creatView{ //用于響應(yīng)掃描事件,點擊開始掃描 button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setFrame:CGRectMake(110, 200, 100, 40)]; [button setTitle:@"掃描" forState:UIControlStateNormal]; [button addTarget:self action:@selector(scanButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; //用于顯示掃描到的圖像 p_w_picpathview = [[UIImageView alloc]initWithFrame:CGRectMake(20, 50, 280, 280)]; [self.view addSubview:p_w_picpathview]; //用于顯示掃描到的信息 label = [[UILabel alloc]initWithFrame:CGRectMake(20, 356, 280, 68)]; [self.view addSubview:label];}
2.掃描二維碼
方法一:直接調(diào)用ZBar提供的ZBarReaderViewController打開一個掃描界面
step1:掃描二維碼操作
-(void)scanButtonPressed:(id)sender{ ZBarReaderViewController *reader = [[ZBarReaderViewController alloc]init]; reader.readerDelegate = self; reader.supportedOrientationsMask = ZBarOrientationMaskAll; ZBarImageScanner *scanner = reader.scanner; [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0]; [self presentViewController:reader animated:YES completion:nil];}
step2:找到二維碼回調(diào)的時候會執(zhí)行ZBarReaderDelegate的對應(yīng)方法
- (void) p_w_picpathPickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info{ //通過info獲得結(jié)果 id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults]; ZBarSymbol *symbol = nil; for(symbol in results) break; <span > </span> p_w_picpathview.p_w_picpath = [info objectForKey: UIImagePickerControllerOriginalImage]; //掃描界面退出 [reader dismissViewControllerAnimated:YES completion:nil];}
方法二:使用ZBar提供的可以嵌在其他視圖中的ZBarReaderView
step1:掃描二維碼操作
-(void)scanButtonPressed:(id)sender{ ZBarReaderView *readerView = [[ZBarReaderView alloc]init]; readerView.frame = CGRectMake(10, 44, 300, 300); readerView.readerDelegate = self; //掃描區(qū)域 CGRect scanMaskRect = CGRectMake(60, CGRectGetMidY(readerView.frame) - 126, 200, 200); //處理模擬器 if (TARGET_IPHONE_SIMULATOR) { ZBarCameraSimulator *cameraSimulator = [[ZBarCameraSimulator alloc]initWithViewController:self]; cameraSimulator.readerView = readerView; } [self.view addSubview:readerView]; //掃描區(qū)域計算 readerView.scanCrop = [self getScanCrop:scanMaskRect readerViewBounds:readerView.bounds]; //調(diào)用ZBarReaderView的start方法開始掃描 [readerView start];}
step2:掃描區(qū)域計算
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds{ CGFloat x,y,width,height; x = rect.origin.x / readerViewBounds.size.width; y = rect.origin.y / readerViewBounds.size.height; width = rect.size.width / readerViewBounds.size.width; height = rect.size.height / readerViewBounds.size.height; return CGRectMake(x, y, width, height);}
step3:在掃描到二維碼回調(diào)的時候會執(zhí)行ZBarReaderViewDelegate的對應(yīng)方法
- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)s fromImage:(UIImage *)p_w_picpath { for (ZBarSymbol *symbol in symbols) { NSLog(@"%@", symbol.data); //將信息顯示在lable上 label.text = symbol.data ; break; } //調(diào)用ZBarReaderView的stop方法停止 [readerView stop]; } 另外有一點需要注意的地方,二維碼掃描所在頁面和其他頁面間的跳轉(zhuǎn)盡量用
SaoMiaoVC *saomiao = [[[SaoMiaoVC alloc]init]autorelease];
saomiao.hidesBottomBarWhenPushed=YES;
saomiao.delegate=self;
UINavigationController *nav= [[[UINavigationController alloc] initWithRootViewController:saomiao] autorelease];
[self presentViewController:nav animated:YES completion:nil]; 這種方式,因為用push 的話容易因為退出或者銷毀得不及時而產(chǎn)生各種各樣的問題!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。