Hey, I have
googled a lot and found very less material for how to implement a QR
code reader in our iPhone app. Here I have integrated all material and
implemented in my project.
So here I am sharing my experience of implementing a QR code reader in our iPhone app using ZBarSDK.
Step 1: Download
ZBarSDK from here.
Step 2: Create a new project in xcode and name it as QRscanner.
Step 3: Integrate this ZBarSDK folder in your xcode project by drag and drop on equal level of class folder in left panel in xcode.
Step 4: Add these 7 frameworks in Frameworks folder:
- AudioToolbox.framework
- CoreMedia.framework
- QuartzCore.framework
- SystemConfiguration.framework
- libiconv.dylib
- AVFoundation.framework
- CoreVideo.framework
Step 5: Now Go To this link and generate some QR codes.
(to generate
QR codes only add the text which you want to attach with QR code at the
end of the link (e.g. I have written archana) and press enter, and right
click to save this image.)
Step 6: Add these generated QR code's image in iphone library.
Step 7: Now open QRscannerViewController.h in your xcode and write this code:
#import
#import "ZBarSDK.h"
@interface QRscannerViewController : UIViewController {
IBOutlet UITextView *resultTextView;
}
@property (nonatomic, retain) IBOutlet UITextView *resultTextView;
@property (nonatomic, retain) UIImagePickerController *imgPicker;
-(IBAction)StartScan:(id) sender;
@end
Step 8: Now open QRscannerViewController.m in your xcode and write this code:
#import "QRscannerViewController.h"
@implementation QRscannerViewController
@synthesize imgPicker,resultTextView;
-(IBAction)StartScan:(id) sender
{
ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
reader.readerView.torchMode = 0;
ZBarImageScanner *scanner = reader.scanner;
// TODO: (optional) additional reader configuration here
// EXAMPLE: disable rarely used I2/5 to improve performance
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
// present and release the controller
[self presentModalViewController: reader
animated: YES];
[reader release];
resultTextView.hidden=NO;
}
- (void) readerControllerDidFailToRead: (ZBarReaderController*) reader
withRetry: (BOOL) retry{
NSLog(@"the image picker failing to read");
}
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
{
NSLog(@"the image picker is calling successfully %@",info);
// ADD: get the decode results
id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
NSString *hiddenData;
for(symbol in results)
hiddenData=[NSString stringWithString:symbol.data];
NSLog(@"the symbols is the following %@",symbol.data);
// EXAMPLE: just grab the first barcode
// break;
// EXAMPLE: do something useful with the barcode data
//resultText.text = symbol.data;
resultTextView.text=symbol.data;
NSLog(@"BARCODE= %@",symbol.data);
NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
[storeData setObject:hiddenData forKey:@"CONSUMERID"];
NSLog(@"SYMBOL : %@",hiddenData);
resultTextView.text=hiddenData;
[reader dismissModalViewControllerAnimated: NO];
}
Step 9: Now open QRscannerViewController.xib and design it UI like this:
Step 10: Now make connection of above 2 objects(UITextView and UIButton) with IBOutlet resultTextView and IBAction StartScan.(Refer Step 7 of…)
At last build and run this app.
How to run this app:
Press "START SCAN" button, and as we are running our app in simulator you will get screen like this:
Now press and hold (alt key)+(left click), Photo Album (image gallery, photo library) will open.
Select
any QR code image. It will take hardly 2-3 seconds for scanning and
finally you will get home screen with scanned data associated with QR code image on text view.