iOS中使用ZBar掃描二維碼自訂掃描介面

來源:互聯網
上載者:User

iOS中使用ZBar掃描二維碼自訂掃描介面
之前在Android中使用過ZXing識別二維碼,ZXing也有對應的iOS版本,經過瞭解,ZBar也是一個常用的二維碼識別軟體,並分別提供了iOS和Android的SDK可供使用,最終我選擇了ZBar進行二維碼識別,它的注釋清晰,便於使用。

ZBar為我們提供了兩種使用方式,一種是直接調用ZBar提供的ZBarReaderViewController開啟一個掃描介面,另一種方式是使用ZBar提供的可以嵌在其他視圖中的ZBarReaderView,實際項目中我們更可能會使用第二種方式,這可以讓我們對介面做更多的定製。

ZBar使用起來也非常簡單,將ZBarSDK匯入項目,在需要使用ZBar的檔案中匯入ZBarSDK.h標頭檔即可

#pragma mark 初始化掃描- (void)InitScan{    readview = [ZBarReaderView new];    readview.backgroundColor = [UIColor clearColor];    readview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);    readview.readerDelegate = self;    readview.allowsPinchZoom = YES;//使用手勢變焦    readview.trackingColor = [UIColor redColor];    readview.showsFPS = NO;// 顯示幀率  YES 顯示  NO 不顯示    //readview.scanCrop = CGRectMake(0, 0, 1, 1);//將被掃描的映像的地區        UIImage *hbImage=[UIImage imageNamed:@"pick_bg.png"];    scanZomeBack=[[UIImageView alloc] initWithImage:hbImage];    //添加一個背景圖片    CGRect mImagerect=CGRectMake((readview.frame.size.width-200)/2.0, (readview.frame.size.height-200)/2.0, 200, 200);    [scanZomeBack setFrame:mImagerect];    readview.scanCrop = [self getScanCrop:mImagerect readerViewBounds:readview.bounds];//將被掃描的映像的地區        [readview addSubview:scanZomeBack];    [readview addSubview:readLineView];    [self.view addSubview:readview];    [readview start];    }

#pragma mark 擷取掃描地區-(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);}

#pragma mark 掃描動畫-(void)loopDrawLine{    CGRect  rect = CGRectMake(scanZomeBack.frame.origin.x, scanZomeBack.frame.origin.y, scanZomeBack.frame.size.width, 2);    if (readLineView) {        [readLineView removeFromSuperview];    }    readLineView = [[UIImageView alloc] initWithFrame:rect];    [readLineView setImage:[UIImage imageNamed:@"line.png"]];    [UIView animateWithDuration:3.0                          delay: 0.0                        options: UIViewAnimationOptionCurveEaseIn                     animations:^{                         //修改fream的代碼寫在這裡                         readLineView.frame =CGRectMake(scanZomeBack.frame.origin.x, scanZomeBack.frame.origin.y+scanZomeBack.frame.size.height, scanZomeBack.frame.size.width, 2);                         [readLineView setAnimationRepeatCount:0];                                              }                     completion:^(BOOL finished){                         if (!is_Anmotion) {                                                          [self loopDrawLine];                         }                                              }];        [readview addSubview:readLineView];    }#pragma mark 擷取掃描結果- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image{    // 得到掃描的條碼內容    const zbar_symbol_t *symbol = zbar_symbol_set_first_symbol(symbols.zbarSymbolSet);    NSString *symbolStr = [NSString stringWithUTF8String: zbar_symbol_get_data(symbol)];    if (zbar_symbol_get_type(symbol) == ZBAR_QRCODE) {        // 是否QR二維碼    }        for (ZBarSymbol *symbol in symbols) {        [sTxtField setText:symbol.data];        break;    }        [readerView stop];    [readerView removeFromSuperview];        }


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.