Zbar sdk扫描仪无法正常工作

时间:2013-03-01 06:41:10

标签: ios objective-c ipad ios5 zbar-sdk

在我的应用程序中,我已经集成了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

在背景下,扫描仪在两次出现的情况下仍然像这样运行..

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题。我在名为BOOL的类中添加了_processing实例变量。然后我这样做了:

- (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

答案 1 :(得分:0)

由于ZBarReaderViewController以连续模式扫描图像,因此在解除ZBarReaderViewController之前可能会扫描图像两次。您可以尝试使读者(ZBarReaderViewController * reader)成为您的类的实例变量,并在委托方法中:

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