Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - How to decode image with zxing

I need to decode an QR image using the zxing library for iOS.

I'm new to iOS programming, and have been looking through the code examples included in the project, but I can't figure out how to just decode an image using this library.

If anyone can please post examples of how I decode an image it would be much appreciated.

So far I've identified the Decoder class, which has a method called "decodeImage" which I've loaded with an image. But this method returns a boolean, and what I need is a text string containing the value of the QR code.

Thanks in advance!

like image 517
klausk Avatar asked Oct 24 '11 22:10

klausk


2 Answers

This was the code that finally solved my problem - thanks to the help from 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

IMPLEMENTATION FILE

#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
like image 93
klausk Avatar answered Sep 25 '22 20:09

klausk


You need to create a delegate class/instance and set the decoder delegate property. Then the widget will call didDecodeImage or failedToDecodeImage when you call decodeImage.

like image 43
smparkes Avatar answered Sep 22 '22 20:09

smparkes