Reputation: 4614
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:
At background the scanner is still running like this in twice occuring case..
Upvotes: 1
Views: 1545
Reputation: 687
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.
}
Upvotes: 0
Reputation: 318934
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.
Upvotes: 3