Implementing a QR Code reader on the iPhone
QR Codes are becoming increasingly popular but few people realize just how easy it is to support this technology from an iPhone application.
In this example, I’ll illustrate how to knock together a simple QR Code reader using the open source ZBarSDK. It’s not going to be an ‘All singing, all dancing’ application, but it will be sufficient to illustrate the point.
The following example assumes that you’re already familiar with both XCode and iPhone development.
Step 1: Create a Window-based iPhone application and name it ‘QRReader’
Step 2: Download ZBarSDK
Step 3: Run the ZBarSDK installer and follow the simple instructions for adding the SDK to your XCode project
Step 4: Add the following frameworks to your project:
- QuartzCore.framework
- AVFoundation.framework
- libiconv.dylib
- CoreVideo.framework
- CoreMedia.framework
- AudioToolbox.framework
Step 5: Add the main view to the Application Delegate, as follows:
At this point ZBarSDK is already integrated with the video feed from the camera, but we still need to implement the delegate so that the SDK can notify us when it has managed to detect a QR Code.
The following delegate method should now be added to the Application Delegate:
Step 6: Next we need to create a new UIViewController subclass and call it ‘QRCodeDetail’. This will be used to show the details of the QR Code once it has been read by the main view.
Update the header file to contain the following:
Remember to synthesize the variables:
Finally we update the ‘viewDidLoad’ method:
Step 7: Hook the QRCodeDetail view into the main view
Back in the Application Delegate we need to import our newly created view:
Then we need to update the ‘didFinishPickingMediaWithInfo’ method to load the ‘QRCodeDetail’ view whenever a QR Code is detected:
Step 8:Â Simply install on an iPhone and enjoy the QR Code Reader.
You can easily generate your own QR Codes here.
Hi, Oscar. Can one run this on the simulator? Can one feed it sample QR codes via a list of .png files? If so, what code should be added please? I’m a newbie in XCode world. Am using XCode 3.2 Simulator 4.1. Hope you can help. Thanks in advance.
Baci,
I’ve always used the ZBar SDK on an actual device, but this link discusses how the SDK can be used on the iPhone Simulator. Hope it helps…