Hi there, today we are going to make a really simple app that scans the Bar Codes and QR Codes using the camera of your iPhone and displays the data stored in the code.
The very first thing that we have to do is download and install the ZBarSDK from the internet.
The ZBarSDK provides the necessary files and resources that are used to scan the codes. The ZBarSDK can be downloaded from the following link.
http://zbar.sourceforge.net/iphone/
Download the .dmk file from the above mentioned link and drag and drop the folder in the target of your project.
Now in the .Xib file of the ViewController drag and drop an imageview on the top half of the view. Then add a UILabel in which the data will be displayed and finally add a round rect button for the camera click.
Now lets get on with some actual coding.
import the "ZBarSDK.h" in the .h file of the ViewController then add ZBarReaderDelegate and UIImagePickerControllerDelegate. Make IBOutlets for a UIImageView and a UILabel. Initialize a cameraBtnClicked IBAction for the button.
@interface QrViewController : UIViewController<ZBarReaderDelegate,UIImagePickerControllerDelegate>
{
IBOutlet UIImageView *qrImage;
IBOutlet UILabel *ansLabel;
}
-(IBAction)CameraBtnClicked:(id)sender;
@end
Moving on to the .m file of the ViewController. The code to perform the scanning the codes and displaying the data is as follows.
- (void)viewDidLoad
{
ansLabel.text = @" Code Information Display.";
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(IBAction)CameraBtnClicked:(id)sender
{
NSLog(@"camera button clicked");
ansLabel.text = @" Scanning the QrCode";
ZBarReaderViewController *codeReader = [[ZBarReaderViewController alloc]init];
codeReader.readerDelegate = self;
codeReader.supportedOrientationsMask = ZBarOrientationMaskAll;
ZBarImageScanner *scanner = codeReader.scanner;
[scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
[self presentViewController:codeReader animated:YES completion:nil];
}
#pragma mark Image picker delegates
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for (symbol in results)
break;
ansLabel.text = symbol.data;
qrImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
you can test the working of the code in the simulator itself. Just download some QR codes and Bar Codes from the net and add them to the camera roll album by dragging and dropping them in the simulator. Then run the application and you will notice that the camera button links your simulator to the camera simulation of the simulator. select the bar codes that we saved just a moment ago and watch in wonder as your app decodes them and displays the information on the screen.
To use the app on your iPhone just change the ImagePickerControllerType and enjoy your work on in real world codes. HAPPY CODING.
0 Comment(s)