在iOS中使用ZBar掃描二維碼

來源:互聯網
上載者:User

標籤:android   http   io   ar   os   使用   sp   for   檔案   

  

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

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

ZBar使用起來也非常簡單,將ZBarSDK匯入項目,在需要使用ZBar的檔案中匯入ZBarSDK.h標頭檔即可,以下是ZBarReaderView的初始化方法:

  1. ZBarReaderView readerView = [[ZBarReaderView alloc]init];
  2. readerView.frame = CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height - 44);
  3. readerView.readerDelegate = self;
  4. //關閉閃光燈
  5. readerView.torchMode = 0;
  6. //掃描地區
  7. CGRect scanMaskRect = CGRectMake(60, CGRectGetMidY(readerView.frame) - 126, 200, 200);
  8. //處理模擬器
  9. if (TARGET_IPHONE_SIMULATOR) {
  10.     ZBarCameraSimulator *cameraSimulator 
  11.         = [[ZBarCameraSimulator alloc]initWithViewController:self];
  12.     cameraSimulator.readerView = readerView;
  13. }
  14. [self.view addSubview:readerView];
  15. //掃描地區計算
  16. readerView.scanCrop = [self getScanCrop:scanMaskRect readerViewBounds:self.readerView.bounds];
  17. [readerView start];
複製代碼

以上代碼需要說明的有以下幾點:

閃光燈設定
我不希望在掃描二維碼時開啟閃光燈,所以將ZBarReaderView的torchMode設為0,你可以將它設定為其他任何合適的值。
掃描地區計算
這點比較重要,我們常用的二維碼掃描軟體的有效掃描地區一般都是中央地區,其他部分是不進行掃描的,ZBar可以通過ZBarReaderView的scanCrop屬性設定掃描地區,它的預設值是CGRect(0, 0, 1, 1),表示整個ZBarReaderView地區都是有效掃描地區。我們需要把掃描地區座標計算為對應的百度分數座標,也就是以上代碼中調用的getScanCrop:readerViewBounds方法,親測沒有問題,如下所示:

  1. -(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds
  2. {
  3.     CGFloat x,y,width,height;
  4.     x = rect.origin.x / readerViewBounds.size.width;
  5.     y = rect.origin.y / readerViewBounds.size.height;
  6.     width = rect.size.width / readerViewBounds.size.width;
  7.     height = rect.size.height / readerViewBounds.size.height;
  8.     return CGRectMake(x, y, width, height);
  9. }
複製代碼

PS:在網上找到很多這個方法都是將橫座標和縱座標交叉,這樣是有問題的,仔細想一下就會明白。

初始化部分完成之後,就可以調用ZBarReaderView的start方法開始掃描了,需要讓你的類實現ZBarReaderViewDelegate協議,在掃描到二維碼時會調用delegate的對應方法。最後,當二維碼已經識別時候,可以調用ZBarReaderView的stop方法停止掃描。如下所示:

  1. - (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
  2. {
  3.     for (ZBarSymbol *symbol in symbols) {
  4.         NSLog(@"%@", symbol.data);
  5.         break;
  6.     }
  7.     [self.readerView stop];
複製代碼

好了,就這麼多,是不是非常簡單?

http://www.apkbus.com/android-127507-1-1.html

在iOS中使用ZBar掃描二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.