2013-04-29 71 views
0

我正在爲Iphone和Ipad開發QR碼閱讀器。我正在使用ZBar庫來讀取qr碼。但是當我專注於任何二維碼時,應用程序什麼都不做。我正在使用在文檔部分的zbar網站上給出的代碼。這裏是我的代碼:ZBar qrcode庫不適用於IOS

ZBarReaderViewController *barReader = [ZBarReaderViewController new]; 
    barReader.readerDelegate = self; 
    [barReader.scanner setSymbology:ZBAR_QRCODE config:ZBAR_CFG_ENABLE to:0]; 
    barReader.readerView.zoom = 1.0; 
    barReader.showsCameraControls = NO; 
    barReader.showsZBarControls = NO; 
    barReader.wantsFullScreenLayout = NO; 
    barReader.title = @"QRCode Reader"; 
    [self presentModalViewController:barReader animated:YES]; 

而且在

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

功能我正在檢索這樣

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 
    ZBarSymbolSet *symbols = [info objectForKey:ZBarReaderControllerResults]; 
    ZBarSymbol *symb = nil; 
    NSString *qrCode = nil; 

    for(symb in symbols) 
    { 
     qrCode = [NSString stringWithString:symb.data]; 
    } 

    NSLog(@"The results = %@",qrCode); 

[self dismissModalViewControllerAnimated:YES]; 

回答

2

這個代碼是對我工作: .h文件中

 ADD delegate protocol 
     <ZBarReaderDelegate> 

.m文件

進行掃描

- (IBAction) scanButtonClick 
    { 

       ZBarReaderViewController *reader = [ZBarReaderViewController new]; 
       reader.readerDelegate = self; 
       reader.supportedOrientationsMask = ZBarOrientationMaskAll; 
       ZBarImageScanner *scanner = reader.scanner; 
       // TODO: (optional) additional reader configuration here 

       // EXAMPLE: disable rarely used I2/5 to improve performance 
       [scanner setSymbology: ZBAR_I25 
         config: ZBAR_CFG_ENABLE 
          to: 0]; 

       // present and release the controller 
       [self presentModalViewController: reader 
         animated: YES]; 
       [reader release]; 
    } 

獲取/ retrive使用:

- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info 
    { 
     // ADD: get the decode results 
     id<NSFastEnumeration> results = 
     [info objectForKey: ZBarReaderControllerResults]; 
     ZBarSymbol *symbol = nil; 
     for(symbol in results) 
      // EXAMPLE: just grab the first barcode 
      break; 

     // EXAMPLE: do something useful with the barcode data 
     resultText.text = symbol.data; 

     // EXAMPLE: do something useful with the barcode image 
     resultImage.image = 
     [info objectForKey: UIImagePickerControllerOriginalImage]; 

     // ADD: dismiss the controller (NB dismiss from the *reader*!) 
     [reader dismissModalViewControllerAnimated: YES]; 
    } 
4

爲什麼你不使用ZBarReaderViewDelegate的QR碼?

- (void)readerView:(ZBarReaderView *)view didReadSymbols:(ZBarSymbolSet *)syms fromImage:(UIImage *)img 
{ 
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 

    for (ZBarSymbol *sym in syms) 
    { 
     qrCode = [NSString sym.data]; 
    } 

    NSLog(@"The results = %@",qrCode); 
} 
+0

當然,你的委託(readerDelegate) – user1226119 2013-12-20 13:00:10

2

默認情況下啓用所有符號系統。您的代碼

[barReader.scanner setSymbology:ZBAR_QRCODE config:ZBAR_CFG_ENABLE to:0]; 

是符號ZBAR_QRCODE的配置ZBAR_CFG_ENABLE設置爲0 (false)。這意味着你實際上禁用QRCodes的掃描。

zlib的文檔上disabling unused symbologies解釋說:

例如,如果你只對QR碼感興趣,禁用 別人。實現這一目標的強大方法是禁用所有符號系統,然後僅重新啓用您需要的那些符號系統。這有助於從 隔離您遇到可能在 圖書館未來的版本中添加新的符號,直到您準備來處理它們:

// disables all symbologies 
[scanner setSymbology: 0 
     config: ZBAR_CFG_ENABLE 
     to: 0]; 

// enables QRCode 
[scanner setSymbology: ZBAR_QRCODE 
     config: ZBAR_CFG_ENABLE 
     to: 1]; 

Here是支持的符號列表。

SAMIR RATHOD的答案有效,因爲只有符號系統ZBAR_I25被禁用。