最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
在iOS中使用ZBar掃描二維碼和條形碼

最近做了個外包項目,里面用到了二維碼掃描和微信支付!之前比較熟悉的是ZXing,但是在Xcode7.1里面發(fā)現(xiàn)竟然莫名的不支持,木有辦法,從網(wǎng)上查了一下還有一種支持二維碼掃描的東西,沒錯就是接下來我要說的東東,二維碼掃描的利器 ZBarSDK。閑言少絮叨,言歸正傳!

10年積累的成都網(wǎng)站制作、成都網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先做網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有敘永免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

1、下載 ZBarSDK 官網(wǎng) https://github.com/bmorton/ZBarSDK

2、導(dǎo)入如下框架

在iOS中使用ZBar掃描二維碼和條形碼

3、在AppDelegetem文件中#import "ZBarSDK.h" 

并且 在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

里面添加下面的代碼

//二維碼讀取

    [ZBarReaderViewclass];

  4、在二維碼掃描界面開始#import "ZBarSDK.h" ,添加

委托

 

 

下面開始介紹掃描步驟:
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 results =    [info objectForKey: ZBarReaderControllerResults];    ZBarSymbol *symbol = nil;    for(symbol in results)        break;  	    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)生各種各樣的問題!


文章名稱:在iOS中使用ZBar掃描二維碼和條形碼
文章源于:http://fisionsoft.com.cn/article/pjhshj.html