Using the Zxing library in iOS [go]

Source: Internet
Author: User

Objective

ZXing (GitHub mirror address) is an open source barcode generation and scanning library (open source protocol for Apache2.0). It supports a wide range of barcode formats and is available in a variety of languages, including: Java, C + +, C #, Objective-c, ActionScript and Ruby.

I used the zxing scan QR code feature in iOS project development last week. Here's a summary of how to integrate zxing into an existing iOS project and share it with everyone.

Integration steps

First go to Google Code or github to download zxing code, the whole project is relatively large, we only need to involve the part of iOS, so it is best to do some cutting. Simply put, we only need to keep the 2 folders of CPP and iphone, and all the rest are erased. As shown in the following:

Then we continue to crop, for the CPP this directory, only keep cpp/core/src/zxing below the content, the rest of the content can also be deleted. However, the entire directory structure must remain intact. 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 visible zxingwidget.xcodeproj file to our iOS project.

Next, we need to set the dependency between the Zxing project and our original iOS project. In the settings for our iOS project, click Build Phases tab, then add Target Dependencies and Link binary, and increase these framework dependencies:

a. AVFoundationb. AudioToolboxc. CoreVideod. CoreMediae. libiconvf. AddressBookg. AddressBookUI

After completion as shown:

In the final step, add the following 2 header search path to the settings:

    • ./zxing/iphone/zxingwidget/classes
    • ./zxing/cpp/core/src

It is important to note that the first path is set to loop to find subdirectories, while the second one does not loop lookups as shown in:

Congratulations, after completing this step, you have completed the integration of the Zxing library. Here is how to use the Zxing library to do two-dimensional code recognition.

Two-dimensional code recognition

The iOS version of Zxing provides 2 ways to do two-dimensional code recognition, the first method is relatively simple, the second method is more complex. I used the first method when I was doing the demo, and I used the second method when I did the real project development, so we all introduced to you.

Use method One

Zxing directly provides a view Controller that scans the QR code, which is Zxingwidgetcontroller. In the interface code you need to use, include file dependencies:

12
#import <ZXingWidgetController.h>#import <QRCodeReader.h> 

Then, when you need to scan, call the following code:

123456789         
-(Ibaction)Scanpressed:(Id)Sender{ Zxingwidgetcontroller*Widcontroller=[[ZxingwidgetcontrollerAlloc]Initwithdelegate:SelfShowcancel:YESOnedmode:NO]; Nsmutableset*Readers=[[NsmutablesetAlloc]Init]; Qrcodereader*Qrcodereader=[[QrcodereaderAlloc]Init]; [ReadersAddObject:qrcodereader [qrcodereader release]; widcontroller. Readers = readers; [readers release]; [self presentmodalviewcontroller:widController animated:yes]; [widcontroller release];                /span>                

When the zxing scan has a result, the following callback function is called:

1234  
 @protocol zxingdelegate- ( Span class= "kt" >void) zxingcontroller: ( Zxingwidgetcontroller*) controller  (nsstring *)  Result; - (void)  (zxingwidgetcontroller* ) controller;  @end                
Use Method Two

The difference between method two and method one is equivalent to the difference between avfoundation and Uiimagepickercontroller. In simple terms, it is more cumbersome to use the method two than the method one, but the gains are more customizable.

Using method Two o'clock, you need to use Avfoundation to obtain the real-time image returned by the camera, and then turn to UIImage, and finally to zxing decoder class to complete the identification of the two-dimensional code. Because of the slightly more code involved in using Avfoundation, I wrote the schematic code as follows:

123456789-ten-19                 the 
#import "Decoder.h"#import "TwoDDecoderResult.h"#import "QRCodeReader.h"-(void)Viewdidload{ Setup QR Reader Self.Qrreader=[[NsmutablesetAlloc]Init]; Qrcodereader*Qrcodereader=[[QrcodereaderAlloc]Init]; [Self.QrreaderAddObject:Qrcodereader]; Self.Scanningqr=NO; Self.Step=Step_qr;}callback function for Avfoundation-(void)Captureoutput:(Avcaptureoutput*)CaptureoutputDidoutputsamplebuffer:(Cmsamplebufferref)SamplebufferFromconnection:(Avcaptureconnection*)Connection{ The first step is to turn Samplebuffer into UIImage UIImage*Image=[SelfGetcaptureimage:Samplebuffer]; Step two, identify the image with decoder Decoder*D=[[DecoderAlloc]init d. Readers = self. Qrreader; d. Delegate = self; self. SCANNINGQR = [d decodeimage: Image] == yes no : yes;                /span>                

The decoder class of zxing provides the following callback function to obtain the recognition result:

123456789        
@protocolDecoderdelegate<NSObject>@Optional-(void)Decoder(Decoder*)DecoderWilldecodeimage:(UIImage*)ImageUsingsubset:(UIImage*)Subset;-(void)Decoder(Decoder*)DecoderDiddecodeimage:(UIImage*)ImageUsingsubset:(UIImage*)SubsetWithresult:(Twoddecoderresult*)Result{ NSLog(@ "result =%@",[ResultText]);}-(void)Decoder(Decoder*)DecoderFailedtodecodeimage:(uiimage *) image usingsubset:< Span class= "P" > (uiimage *) subset reason: (nsstring *) reason; - (voiddecoder:  (decoder *) decoder foundpossibleresultpoint: (cgpoint) point;  @end                
Trouble Shoot & Tips

I have encountered some problems in the use, mainly is the problem of compiling.

    1. One is not finding the head file. Workaround: Change the source file extension used to zxing from. m to. mm.
    2. Error: Undefined symbols for Architecture armv7s, workaround: Put zxingwidget a Build target parameter: "Build Active architecture only" modified to "NO "C + + standard libray:libstdc++ (GNU C + + standard library)
    3. Error: No such file or directory, it may be your header Search path is wrong, or your zxing library directory structure is not what I emphasized above, check it carefully.
    4. If you need to generate two-dimensional code to do the test, recommend a good online generation QR Code of the website: http://cli.im/
Compatibility issues with Zxing and OPENCV

ZXing 2.1 and OpenCV 2.4.3 have some compatibility issues with their iOS libraries, they have some requirements for the version of the C + + standard library and the compiler version, resulting in the satisfaction of one party and the other on the compilation. Someone at StackOverflow finally found a way to make them peaceful, but only for iOS5.0 and above. Just as our app only supports ios5.0+, so it's done. So if you happen to encounter this problem, you can refer to this post.

http://blog.devtang.com/blog/2012/12/23/use-zxing-library/

Using the Zxing library in iOS [go]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.