Preface
Zxing (GitHub image address) is an open source library for generating and scanning bar codes (the Open Source protocol is apache2.0 ). It not only supports a large number of bar code formats, but also provides implementation versions in a variety of languages, including Java, C ++, C #, objective-C,
ActionScript and Ruby.
I used the zxing QR code scanning function in IOS project development last week. Here we will summarize how to integrate zxing into an existing IOS project and share it with you.
Integration steps
First, go to Google Code or GitHub to download the zxing code. The whole project is large. We only need the part that involves iOS, so we 'd better make some cropping. Simply put, we only need to keep the CPP and iPhone folders, and delete all the other folders. As shown in:
Next, we continue to crop the directory CPP. For the CPP directory, only the content under CPP/CORE/src/zxing is retained, and the rest of the content can be deleted. However, the entire directory structure must be unchanged. After cropping, the entire directory structure is as follows:
Next, we move the cropped zxing directory to the directory of our IOS project, and drag the zxingwidget. xcodeproj file that we can see in to our IOS project.
Next, we need to set the dependency between the zxing project and our original IOS project. In settings of our IOS project, click build phases tab, add target dependencies and link binary, and add these framework dependencies:
a. AVFoundation
b. AudioToolbox
c. CoreVideo
d. CoreMedia
e. libiconv
f. AddressBook
g. AddressBookUI
After completion, as shown:
In the last step, add the following 2 header search paths to the settings:
./zxing/iphone/ZXingWidget/Classes
./zxing/cpp/core/src
It should be noted that the first path should be set to loop through subdirectories, while the second path does not loop through, as shown:
Congratulations, after completing this step, you have completed the integration of the ZXing library. Let's talk about how to use the ZXing library to do QR code recognition.
QR code recognition
The iOS version of ZXing provides 2 methods for QR code recognition. The first method is simpler and the second method is more complicated. I used the first method when doing Demo, and the second method when doing real project development, so I will introduce it to everyone.
Method one
ZXing directly provides a View Controller that scans the QR code, which is ZXingWidgetController. In the interface code to be used, add file dependencies:
#import <ZXingWidgetController.h>
#import <QRCodeReader.h>
Then when you need to scan, call the following code:
-(IBAction) scanPressed: (id) sender {
ZXingWidgetController * widController = [[ZXingWidgetController alloc] initWithDelegate: self showCancel: YES OneDMode: NO];
NSMutableSet * readers = [[NSMutableSet alloc] init];
QRCodeReader * qrcodeReader = [[QRCodeReader alloc] init];
[readers addObject: qrcodeReader];
[qrcodeReader release];
widController.readers = readers;
[readers release];
[self presentModalViewController: widController animated: YES];
[widController release];
}
When the ZXing scan has a result, the following callback function will be called:
@protocol ZXingDelegate
-(void) zxingController: (ZXingWidgetController *) controller didScanResult: (NSString *) result;
-(void) zxingControllerDidCancel: (ZXingWidgetController *) controller;
@end
Method two
The difference between method two and method one is equivalent to the difference between AVFoundation and UIImagePickerController. To put it simply, it is more troublesome to use method two than method one, but it is more customizable.
When using method two, you need to use AVFoundation to get the real-time image returned by Camera, and then convert it to UIImage, and finally pass it to the Decoder class of ZXing to complete the identification of the two-dimensional code. Due to the slightly more code involved in using AVFoundation, the schematic code I wrote is as follows:
#import "Decoder.h"
#import "TwoDDecoderResult.h"
#import "QRCodeReader.h"
-(void) viewDidLoad {
// setup QR reader
self.qrReader = [[NSMutableSet alloc] init];
QRCodeReader * qrcodeReader = [[QRCodeReader alloc] init];
[self.qrReader addObject: qrcodeReader];
self.scanningQR = NO;
self.step = STEP_QR;
}
// AVFoundation callback function
-(void) captureOutput: (AVCaptureOutput *) captureOutput didOutputSampleBuffer: (CMSampleBufferRef) sampleBuffer fromConnection: (AVCaptureConnection *) connection {
// The first step is to convert sampleBuffer to UIImage
UIImage * image = [self getCaptureImage: sampleBuffer];
// The second step is to use Decoder to identify the image
Decoder * d = [[Decoder alloc] init];
d.readers = self.qrReader;
d.delegate = self;
self.scanningQR = [d decodeImage: image] == YES? NO: YES;
}
ZXing's Decoder class provides the following callback functions to obtain recognition results:
@protocol DecoderDelegate <NSObject>
@optional
-(void) decoder: (Decoder *) decoder willDecodeImage: (UIImage *) image usingSubset: (UIImage *) subset;
-(void) decoder: (Decoder *) decoder didDecodeImage: (UIImage *) image usingSubset: (UIImage *) subset withResult: (TwoDDecoderResult *) result {
NSLog (@ "result =% @", [result text]);
}
-(void) decoder: (Decoder *) decoder failedToDecodeImage: (UIImage *) image usingSubset: (UIImage *) subset reason: (NSString *) reason;
-(void) decoder: (Decoder *) decoder foundPossibleResultPoint: (CGPoint) point;
@end
Trouble Shoot & Tips
I encountered some problems in use, mainly the problem of compilation.
One is that the header file cannot be found. Solution: Change the extension of the source file used by ZXing from .m to .mm.
Error: Undefined symbols for architecture armv7s, solution: modify a build target parameter of ZXingWidget: "Build Active Architecture Only" to
"NO".
Error: No such file or directory. If this error occurs, it may be that your Header Search Path is written incorrectly, or that the directory structure of your zxing library is not what I emphasized above. Check it carefully.
If you need to generate a QR code for testing, we recommend a good website for generating QR codes online: http://cli.im/
ZXing and OpenCV compatibility issues
The iOS libraries of ZXing 2.1 and OpenCV 2.4.3 have some compatibility issues. They have some requirements for the version of the C ++ standard library and the version of the compiler. As a result, if one party is satisfied, the other party will fail to compile. Someone on Stackoverflow finally found a way to make them coexist peacefully, but only for iOS5.0 and above. Our app just supports iOS5.0 +, so we are done. So if you happen to encounter this problem, you can refer to this post.
Hope this article is useful to everyone, Have Fun ~
Posted by Tang Qiao Dec 23rd, 2012
iOS
http://blog.devtang.com/blog/2012/12/23/use-zxing-library/