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!
This was the code that finally solved my problem - thanks to the help from smparkes
#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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With