简体   繁体   English

使用ZBar扫描和捕获条形码

[英]Scanning and capturing barcode with ZBar

Trying to use ZBar to capture a barcode. 尝试使用ZBar捕获条形码。 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. 扫描时没有任何内容输出到控制台,所以不要认为根本没有调用didReadSymbols。

.h 。H

@interface ScannerViewController : UIViewController <ZBarReaderDelegate> {
}

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

.m .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. 干杯。

When I look at the documentation for ZBar, I see the delegate method signature is : 当我查看ZBar的文档时, 我看到委托方法签名是

- (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. 更换你的“ zbr与”“ readerView ”和你的委托方法应该被调用。

I added the below to the ScannerViewController interface. 我将以下内容添加到ScannerViewController界面。

ZBarReaderViewController *reader;

I then changed the readerView method for the below, and it works perfectly. 然后我改变了下面的readerView方法,它完美无缺。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM