2013-03-01 74 views
1

在我的應用程序中,我已經集成了Zbar SDK掃描儀,雖然通常掃描它的工作正常,但我的情況是有些時候didfinishpickingmediawithInfo:委託方法發射兩次。這是我的代碼,是在一個單獨的課。Zbar sdk掃描儀不能正常工作

-(void)scanProductBarCode 
{ 

     ZBarReaderViewController *reader = [ZBarReaderViewController new]; 
     reader.readerDelegate = self; 


     if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
      reader.supportedOrientationsMask = ZBarOrientationMaskLandscape; 
     else 
      reader.supportedOrientationsMask = ZBarOrientationMaskPortrait; 

     ZBarImageScanner *scanner = reader.scanner; 
     [scanner setSymbology: ZBAR_UPCA config: ZBAR_CFG_ENABLE to: 1]; 
     [scanner setSymbology: ZBAR_CODE39 config: ZBAR_CFG_ADD_CHECK to: 0]; 


} 

#pragma mark - Scanner delegate methods 

- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info 
{ 
    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults]; 
    ZBarSymbol *symbol = nil; 
    for(symbol in results) 
     break; 

    barCodeString = [[NSString alloc] initWithString:symbol.data]; 

    if(self.delegate) 
     [self.delegate getBarcodeString:barCodeString]; 

    [reader dismissModalViewControllerAnimated:YES]; 


} 

看到這樣的畫面截圖:

enter image description here

在後臺掃描仍在運行像這樣在兩次存在的情況下..

回答

3

我遇到了同樣的問題。我在我的類_processing中添加了一個BOOL實例變量。然後我這樣做:

- (void)imagePickerController:(UIImagePickerController*)reader didFinishPickingMediaWithInfo:(NSDictionary*)info 
{ 
    if (_processing) return; 

    id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults]; 
    ZBarSymbol *symbol = nil; 
    for(symbol in results) { 
     _processing = YES; 
     barCodeString = symbol.data; 

     if(self.delegate) { 
      [self.delegate getBarcodeString:barCodeString]; 
     } 

     break; 
    } 

    [reader dismissModalViewControllerAnimated:YES]; 
} 

這確保只處理第一個呼叫。如果您計劃重複使用視圖控制器,您可能需要重置_processing

+0

謝謝,但加入這個布爾標誌後,委託方法gerbarcodeString:在觸發一次。但是,在兩次觸發didFinishPickingMediaInfo:委託方法的情況下,首次完成條形碼掃描,並且掃描器視圖仍然不會從應用程序中退出。請參閱上面添加的圖像。 – Ganapathy 2013-03-01 08:35:47

0

由於ZBarReaderViewController以連續模式掃描圖像,因此在關閉ZBarReaderViewController之前可能會掃描圖像兩次。您可以嘗試使讀者(ZBarReaderViewController *閱讀器)類的一個實例變量,並在委託方法:

- (void)imagePickerController:(UIImagePickerController*)reader didFinishPickingMediaWithInfo:(NSDictionary*)info 
{ 
    // Stop further scanning 
    [reader.readerView stop]; 
    ... 
    //Continue with processing barcode data. 
}