之前在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]; }
github地址:https://github.com/ZBar/ZBar
以上所述是小編給大家介紹的iOS中使用ZBar掃描二維碼自定義掃描界面,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!