国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS中使用ZBar掃描二維碼自定義掃描界面功能

2019-10-21 18:48:34
字體:
供稿:網(wǎng)友

之前在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)站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 祥云县| 广德县| 东方市| 伊春市| 平阴县| 株洲县| 龙州县| 潢川县| 朔州市| 雷山县| 长春市| 会东县| 陈巴尔虎旗| 金寨县| 昭通市| 贵州省| 石阡县| 全州县| 日照市| 尤溪县| 龙泉市| 兴安盟| 六安市| 盘锦市| 任丘市| 鸡泽县| 石河子市| 金秀| 宁阳县| 嘉兴市| 林周县| 呼图壁县| 通榆县| 九江市| 锦屏县| 高邮市| 综艺| 许昌县| 河西区| 微山县| 三原县|