📜 ⬆️ ⬇️

Scan and recognize QR codes from our iOS application

QR codes today are used almost everywhere you can imagine. It is quite obvious that many developers would be interested to learn how to organize scanning and recognition of QR codes in their applications for mobile devices.

In the process of developing my own application for iOS, I encountered the fact that there is very little information in Russian on the topic of processing QR codes on the Internet. Having dealt with the application itself, I decided that it was necessary to correct this blatantly unfair situation. Under the cat you will find a description of the development process of an extremely simple application that recognizes QR codes and is of interest to beginner iOS developers. This note assumes that you have basic knowledge of the features of working in Xcode and programming under iOS.

Development will be conducted for Xcode 4.1 , for earlier versions of Xcode (starting with version 3.2.3 ) the process will be almost the same.
The task of scanning and pattern recognition, generally speaking, is rather complicated from the point of view of mathematical and technical implementation. In other words, it is quite difficult to develop an application that recognizes a bar code or a QR code from scratch. Fortunately for us, progress does not stand still and most of the difficult work of scanning and recognition has already been decided by someone. What we use.
So, for work you will need:

Since the iOS device emulator, distributed with Xcode, does not know how to emulate a camera, to test the application, we need a real device - iPhone , iPod Touch or iPad 2 , i.e. Any device with a camera, released in the last 2 years is quite a fit.

Also, for work, we will use the SDK for scanning and recognizing QR codes, which will take on itself the processing of a graphic image of a QR code and its conversion into text information. There are several ready-made open-source libraries offering similar functionality, we will use the SDK ZBar iPhone SDK version 1.2 (the version above 1.2 will do), which can be downloaded here .
')
For this link, you will download the ZBarSDK-1.2.dmg image , inside which we will be interested in the ZBarSDK folder, which contains the library we need.

So let's get started.

1. Launch Xcode, create a new project “View-based Application” in it , call it QR Scanner , and save it somewhere in a place convenient for you on the disk.

2. In the Project navigator, open QR_ScannerViewController.xib .

3. Place a Round Rect Button on the form and place on it, for example, the text “Scan QR code” .

4. Place the Image View element in the upper part of the form, set the view mode property in Aspect Fit in the Object Inspector.

5. Place the Text View element at the bottom of the form, remove the blank text message, instead place, for example, the text “To start scanning the QR code, press the button at the bottom of the screen” . Remove the User Interaction Enabled checkbox in the Object Inspector.

6. Add the outlets to the controller code, for this we open QR_ScannerViewController.h and bring it to this form:
#import <UIKit/UIKit.h> @interface QR_ScannerViewController : UIViewController { UITextView *resultText; UIImageView *resultImage; } @property (nonatomic, retain) IBOutlet UIImageView *resultImage; @property (nonatomic, retain) IBOutlet UITextView *resultText; - (IBAction)scanButtonTapped; @end 


As can be seen from the code, we declared two outlets on the UITextView and UIImageView controls , which we placed on our form. In addition, we announced IBAction, which will process the pressing of our button and scan the QR code.

7. Open the QR_ScannerViewController.xib and link the created outlets and the scanButtonTapped action with the corresponding form elements:

8. Now we need to create an implementation of the objects declared in the QR_ScannerViewController class header file. Open the file QR_ScannerViewController.m and bring it to the following form:
 #import "QR_ScannerViewController.h" @implementation QR_ScannerViewController @synthesize resultImage; @synthesize resultText; - (IBAction)scanButtonTapped{ NSLog(@"Now we are scanning QR-code..."); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];} - (void)viewDidUnload { [super viewDidUnload]; self.resultText = nil; self.resultImage = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);} - (void) dealloc { [resultImage release]; [resultText release]; [super dealloc]; } 

If you are not very familiar with developing applications in Xcode, do not worry - the development environment itself has added most of the code to this file. We just implemented the objects and actions declared in the header file:
 @synthesize resultImage; @synthesize resultText; - (IBAction)scanButtonTapped{ NSLog(@"Now we are scanning QR-code..."); } 

In addition, we added code to free the outlets when the form is unloaded from memory (viewDidUnload):
 self.resultText = nil; self.resultImage = nil; 

Also free up memory in dealloc:
 [resultImage release]; [resultText release]; 


9. Now we need to directly include the previously downloaded ZBarSDK library in our application. To do this, open the disk image ZBarSDK-1.2.dmg , find the folder ZBarSDK in it and drag it from Finder to your project in Xcode.

10. Next, we need to add a few additional libraries to our project. In the Target properties of our project, open the Build Phases tab, open the Link Binary With Libraries drop-down list and click on the plus sign in the lower left part of this list, add the following libraries:

11. Import the header file of our SDK. To do this, open the “QR Scanner-Prefix.pch” file and add the line to it:
  #import "ZBarSDK.h" 


12. For our controller class QR_ScannerViewController, we will declare support for the delegate protocol <ZBarReaderDelegate> , for this we fix the class declaration in the QR_ScannerViewController.h file:
 @interface QR_ScannerViewController : UIViewController <ZBarReaderDelegate>{ UITextView *resultText; UIImageView *resultImage; } 


13. Now we are fully prepared to make the application start doing something useful. In the QR_ScannerViewController.m file , change the scanButtonTapped code to the following:
 - (IBAction)scanButtonTapped{ //  ,     ZBarReaderViewController *reader = [ZBarReaderViewController new]; reader.readerDelegate = self; reader.supportedOrientationsMask = ZBarOrientationMaskAll; ZBarImageScanner *scanner = reader.scanner; /*     QR-.          -… */ [scanner setSymbology:0 config:ZBAR_CFG_ENABLE to:0]; /* …     QR-: */ [scanner setSymbology:ZBAR_QRCODE config:ZBAR_CFG_ENABLE to:1]; reader.readerView.zoom = 1.0; /*        (),  QR-: */ [self presentModalViewController:reader animated:YES]; [reader release]; } 

Now, after clicking on the button in the application, this method will be called, which will directly invoke the view that will display the picture from the camera. It will be enough for the user to point the camera at the QR code, after which it will be read and processed.

14. However, it is not enough to read and process the QR code, it is also necessary to show the user the processing results. To do this, we need to implement the following delegate method (all in the same QR_ScannerViewController.m ):
 - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { /*     info     - «»: */ id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults]; /*         ,          QR-: */ ZBarSymbol *symbol = nil; for(symbol in results) //     break; /*             resultText: */ resultText.text = symbol.data; /*    QR-    UIImageView   resultImage: */ resultImage.image = [info objectForKey: UIImagePickerControllerOriginalImage]; //  ,     QR- [picker dismissModalViewControllerAnimated:YES]; } 

After the operation of this method, the view that photographed the QR code will be removed from the screen and the original view that we developed in the QR_ScannerViewController.xib file is shown . At this presentation you can see the photo of the QR-code and the text of this QR-code.

15. That's it! Next, save the project, compile, run on the device.

16. To scan a QR code, click on the “Scan QR Code” button and point the camera at the image of the QR code. The program will automatically “recognize” the QR code, take a photo of it and recognize it, and then display the information contained in it on the screen.

The description of the process of launching applications on the device, we left behind the scenes, because, firstly, it is a long process from the point of view of description, and, secondly, this article assumes that the reader has some information base in terms of working with Xcode, which implicitly implies the ability to run developed applications on a physical device. However, if readers have a desire, I can describe the process of organizing the launch of developed applications on a physical device in a separate note.

I also note that the library we used allows us to recognize not only QR codes, but also many other types of two-dimensional codes. We deliberately excluded from consideration this functionality in order to concentrate on the task of recognizing QR codes, as well as to stimulate independent study of this library by those who are interested in it.

In conclusion, some useful links on the topic of this note:
1. Site of the library developers ZBarSDK .
2. Wikipedia about QR codes

Source: https://habr.com/ru/post/130814/


All Articles