1

在我的应用程序中,我集成了 Zbar SDK 扫描器,虽然扫描通常可以正常工作,但我的情况有时是 didfinishpickingmediawithInfo: 委托方法触发两次。这是我在单音类中的代码。

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


}

看这个屏幕截图:

在此处输入图像描述

在后台,扫描仪在两次发生的情况下仍然像这样运行..

4

2 回答 2

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如果您计划多次重用视图控制器,则可能需要重置。

于 2013-03-01T06:51:57.603 回答
0

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

- (void)imagePickerController:(UIImagePickerController*)reader didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    // Stop further scanning
    [reader.readerView stop];
    ...
    //Continue with processing barcode data.
}
于 2013-03-02T12:19:38.913 回答