之前在Android中使用過ZXing識(shí)別二維碼,ZXing也有對(duì)應(yīng)的iOS版本,經(jīng)過了解,ZBar也是一個(gè)常用的二維碼識(shí)別軟件,并分別提供了iOS和Android的SDK可供使用,最終我選擇了ZBar進(jìn)行二維碼識(shí)別,它的注釋清晰,便于使用。
ZBar為我們提供了兩種使用方式,一種是直接調(diào)用ZBar提供的ZBarReaderViewController打開一個(gè)掃描界面,另一種方式是使用ZBar提供的可以嵌在其他視圖中的ZBarReaderView,實(shí)際項(xiàng)目中我們更可能會(huì)使用第二種方式,這可以讓我們對(duì)界面做更多的定制。
ZBar使用起來也非常簡(jiǎn)單,將ZBarSDK導(dǎo)入項(xiàng)目,在需要使用ZBar的文件中導(dǎo)入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;//使用手勢(shì)變焦 readview.trackingColor = [UIColor redColor]; readview.showsFPS = NO;// 顯示幀率 YES 顯示 NO 不顯示 //readview.scanCrop = CGRectMake(0, 0, 1, 1);//將被掃描的圖像的區(qū)域 UIImage *hbImage=[UIImage imageNamed:@"pick_bg.png"]; scanZomeBack=[[UIImageView alloc] initWithImage:hbImage]; //添加一個(gè)背景圖片 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];//將被掃描的圖像的區(qū)域 [readview addSubview:scanZomeBack]; [readview addSubview:readLineView]; [self.view addSubview:readview]; [readview start];}#pragma mark 獲取掃描區(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);}#pragma mark 掃描動(dòng)畫-(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 獲取掃描結(jié)果- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image{ // 得到掃描的條碼內(nèi)容 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];}github地址:https://github.com/ZBar/ZBar
以上所述是小編給大家介紹的iOS中使用ZBar掃描二維碼自定義掃描界面,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VEVB武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注