3

Trying to use ZBar to capture a barcode. I have the following code in place at the moment. The scanner shows, and appears to scan the barcode as the green overlay appears around the code. I don't know how to capture the decoded results.

I'm probably going about it wrong, so thought I'd ask. Nothing is output to the console when scanning, so don't think the didReadSymbols is being called at all.

.h

@interface ScannerViewController : UIViewController <ZBarReaderDelegate> {
}

@property (strong, nonatomic) IBOutlet UILabel *readerResult;
@property (strong, nonatomic) IBOutlet UIView *readerView;
@property (strong, nonatomic) IBOutlet ZBarReaderView *zbr;

.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // force class to load so it may be referenced directly from nib
    [ZBarReaderViewController class];

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

    ZBarImageScanner *scanner = reader.scanner;
    //reader.cameraOverlayView = self.readerView;
    [scanner setSymbology: 0
                          config: ZBAR_CFG_ENABLE
                              to: 1];
    [reader setShowsZBarControls:NO];
    [reader.readerView start];
    self.zbr = reader.readerView;
    [self.view addSubview:reader.view];

}

- (void) zbr: (ZBarReaderView*) view
     didReadSymbols: (ZBarSymbolSet*) syms
          fromImage: (UIImage*) img
{
    NSLog(@"Scanner used");
    //do something useful with results and display resultText in resultViewController
    for(ZBarSymbol *sym in syms) {
        NSLog(@"Logged");
        //return resultText;
        break;
    }
}

Any advice would be great. I'm getting very confused with this at the moment. Cheers.

Luke Smith
  • 682
  • 6
  • 18

2 Answers2

0

When I look at the documentation for ZBar, I see the delegate method signature is:

- (void) readerView:(ZBarReaderView*)readerView didReadSymbols:(ZBarSymbolSet*)symbols fromImage:(UIImage*)image

which is not the same thing as what you have above. Replace your "zbr" with "readerView" and your delegate method should get called.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

I added the below to the ScannerViewController interface.

ZBarReaderViewController *reader;

I then changed the readerView method for the below, and it works perfectly.

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    UIImage *image =
    [info objectForKey: UIImagePickerControllerOriginalImage];
    NSString *resultText = [[NSString alloc] init];
    for(ZBarSymbol *sym in results) {

        NSLog(@"%@", sym.data);
        resultText = sym.data;
        //return resultText;
        break;
    }
}
Luke Smith
  • 682
  • 6
  • 18