Many iPhone developers want their application to support automatic recognition of barcodes from the camera feed in real-time. ZBar makes this easy!
There are three levels that you may choose to integrate at, from least complex (recommended) to most complex these are:
This is the fastest, easiest and recommend way to get the barcode reader into your application. It is also the only way to support automatic fallback for iOS 3.1. The procedure is the same as using a UIImagePickerController to take a picture with the camera, so it will help if you are familiar with that. Basically you:
Create the reader.
This is as simple as creating a new ZBarReaderViewController:
ZBarReaderViewController *reader = [ZBarReaderViewController new];
Setup a delegate to receive the results.
The delegate should implement the ZBarReaderDelegate protocol, which inherits from UIImagePickerControllerDelegate:
reader.readerDelegate = self;
Configure the reader.
Aside from the properties of the reader itself, you can configure the decoder via the scanner property and further customize the view via the readerView property:
[reader.scanner setSymbology: ZBAR_QRCODE
config: ZBAR_CFG_ENABLE
to: 0];
reader.readerView.zoom = 1.0;
See Customizing the Interface and Optimizing the Reader for more details.
Present the reader to the user.
Typically the controller is presented modally, although the new controller does not require it (note that modal presentation is the only option if you want to support iOS 3.1 fallback):
[self presentModalViewController: reader
animated: YES];
Process the results.
The controller will call the imagePickerController:didFinishPickingMediaWithInfo: method of your delegate every time new results become available. The barcode data can be obtained using the ZBarReaderControllerResults key of the info dictionary. This key will return “something enumerable”; keep in mind that there may be multiple results. You may also retrieve the corresponding image with UIImagePickerControllerOriginalImage as usual:
- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
{
id<NSFastEnumeration> results =
[info objectForKey: ZBarReaderControllerResults];
UIImage *image =
[info objectForKey: UIImagePickerControllerOriginalImage];
...
The reader parameter will be the actual type of the reader (not necessarily a UIImagePickerController).
Note
The delegate method should queue the interface response and return as soon as possible; any processing of the results should be deferred until later, otherwise the user will experience unacceptable latency between the actual scan completion and the visual interface feedback.
Dismiss the reader (or not).
Once you have the results you may dismiss the reader:
[reader dismissModalViewControllerAnimated: YES];
Warning
It is very important to dismiss from the reader (not the presenting controller) to avoid corrupting the interface.
Alternatively, you may choose to continue scanning and provide visual feedback another way (eg, maybe by updating your custom overlay with the results). The “continuous” mode of the readertest example does this.
ZBarReaderViewController is a relatively thin wrapper around a ZBarReaderView; it is possible to use the view directly, even from Interface Builder. You will lose the automatic fallback for iOS 3.1 and some of the simulator and rotation hooks. The documentation is also less complete, so you need to be able to UTSL. See the EmbedReader sample for a working example.
If you have special requirements for the capture session or just want to use your own preview, you can add your own ZBarCaptureReader to your session. You must have a solid understanding of the AVCapture infrastructure if you plan to use this approach.
TBD
sorry, you’re on your own here - UTSL :)