1

In my application i have integrated Zbar SDK scanner, while scanning usually its working fine but my case is some times didfinishpickingmediawithInfo: delegate method firing twice. Here is my code which is in a singletone class.

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


}

See this screen shot:

enter image description here

At background the scanner is still running like this in twice occuring case..

Ganapathy
  • 4,594
  • 5
  • 23
  • 41

2 Answers2

3

I ran into the same problem. I added a BOOL instance variable to my class named _processing. Then I did this:

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

This ensures that only the first call is processed. You may need to reset _processing if you plan to reuse the view controller more than once.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks, but after adding this bool flag, the delegate method gerbarcodeString: is firing one time only. But, in twice firing the didFinishPickingMediaInfo: delegate method case, at first time barcode scanning is done and still the scanner view is not resigned from the application. Please see the image added above. – Ganapathy Mar 01 '13 at 08:35
0

Since the ZBarReaderViewController scans the image in continuous mode, it could be that the image is scanned twice before you dismiss the ZBarReaderViewController. You may try making the reader (ZBarReaderViewController *reader ) an instance variable of your class, and in the delegate method:

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