iOS - 如何使用zxing解码图像

时间:2011-10-24 22:37:05

标签: ios image decode qr-code zxing

我需要使用适用于iOS的zxing库解码QR图像。

我是iOS编程的新手,并且一直在查看项目中包含的代码示例,但我无法弄清楚如何使用此库解码图像。

如果有人可以发布我如何解码图像的例子,我将不胜感激。

到目前为止,我已经确定了Decoder类,它有一个名为“decodeImage”的方法,我已经加载了一个图像。但是这个方法返回一个布尔值,我需要的是一个包含QR码值的文本字符串。

提前致谢!

2 个答案:

答案 0 :(得分:6)

这是最终解决了我的问题的代码 - 感谢smparkes的帮助

HEADER FILE

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "ApplicationConfiguration.h"
#import <ZXingWidgetController.h> 

@interface ScanViewController : UIViewController<DecoderDelegate>
{
    UIButton *scanButton;
}

@property (nonatomic, retain) IBOutlet UIButton *scanButton;
@property (nonatomic, retain ) NSSet *readers;

- (IBAction)doScanAction;
- (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)result;
- (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason;

@end

实施文件

#import "ScanViewController.h"
#import <ZXingWidgetController.h> 
#import <QRCodeReader.h> 
#import "TwoDDecoderResult.h"

@implementation ScanViewController

@synthesize scanButton;
@synthesize readers;

-(IBAction)doScanAction{
    QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
    self.readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];
    [qrcodeReader release];

    Decoder *d = [[Decoder alloc] init];
    [d setDelegate:self];
    [d setReaders:self.readers];
    [readers retain];

    BOOL decodeSuccess= [d decodeImage:[UIImage imageNamed:@"QRcode.png"]];
    NSLog(@"BOOL = %@\n", (decodeSuccess ? @"YES" : @"NO"));
}

- (void)decoder:(Decoder *)decoder didDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset withResult:(TwoDDecoderResult *)result{
    [result retain];
    NSLog(@"Did Decode Image Result: %d",[result text]);
    [result release];
}

- (void)decoder:(Decoder *)decoder failedToDecodeImage:(UIImage *)image usingSubset:(UIImage *)subset reason:(NSString *)reason;
{
    [reason retain];
    NSLog(@"Failed Decode Image Result: %d",reason);
    [reason release];
}

@end

答案 1 :(得分:5)

您需要创建委托类/实例并设置解码器delegate属性。然后,当您致电didDecodeImage时,小工具会拨打failedToDecodeImagedecodeImage