Demo download: http://download.csdn.net/download/qingsi00/5001589
Today, QR codes are everywhere, and QR codes are indispensable for physical products and coupons.
Mobile devices such as mobile phones have become a good application platform for QR codes, whether generating or scanning QR codes.
The following describes how to use a QR code on an apple iOS device:
1. Scan the QR code (Decoding)
There are many open-source libraries for scanning QR codes, such as zbar and zxing. Here we use zbar as an example to build a QR code scanning application.
First download the zbar SDK https://github.com/bmorton/ZBarSDK on GitHub
Open the compressed package. The zbarsdk folder contains the zbarsdk folder in the project.
(Finder)
(Xcode)
It contains a static library of libzbar..
Add the Framework framework and link library (dynamic library and static library) to the project ). In the project properties targets-> summary, find linked frameworks and libraries
Add avfoundation. framwork, coremedia. Framework, corevideo. Framework, libiconv. dylib, and libzbar.
()
Then introduce the header file where it is used.
# Import "zbarsdk. H"
The zbarreaderdelegate protocol must be implemented for the class that calls zbar.
For example, uiviewcontroller <zbarreaderdelegate>
The source file is as follows:
-(Bool) Application: (uiapplication *) Application
Didfinishlaunchingwitexceptions: (nsdictionary *) Options
{
Self. Window. rootviewcontroller = self. tabbarcontroller;
[Self. Window makekeyandvisible];
// Force class to load so it may be referenced directly from nib
[Zbarreaderviewcontroller
Class];
Zbarreaderviewcontroller * reader =
[Self. tabbarcontroller. viewcontrollers
Objectatindex: 0];
Reader. readerdelegate =
Self;
Reader. showszbarcontrols =
No;
Reader. supportedorientationsmask =
Zbarorientationmaskall;
Return (yes );
}
// Zbarreaderdelegate
-(Void) imagepickercontroller: (uiimagepickercontroller *) picker
Didfinishpickingmediawithinfo: (nsdictionary *) info
{
// Do something useful with results
Uitabbarcontroller * tabs =
Self. tabbarcontroller;
Tabs. selectedindex =
1;
Resultsviewcontroller * Results = [tabs. viewcontrollers
Objectatindex: 1];
Uiimage * image = [info
Objectforkey:
Uiimagepickercontrolleroriginalimage];
Results. resultimage. Image = image;
ID <nsfastenumeration> Syms =
[Info objectforkey:
Zbarreadercontrollerresults];
For (zbarsymbol * sym
In Syms ){
Results. resulttext. Text = sym. Data;
Break;
}
}
2. Generate a QR code)
Qrencoder is easier to use in the library that generates the QR code. The Android version of The zxing library is a Java-based encoding library, but I do not know why the C ++ version does not exist. Therefore, it cannot be decoded on IOS, if the application needs to generate a QR code and scan the QR code, there may be two sets of different libraries.
First, download it to the SDK library on GitHub.
Address https://github.com/jverkoey/ObjQREncoder
Decompress the package and directly include the source code library in the project ()
Set the header file path. In the target, create settings search path
Of course, you can also directly compile the library into the static library libqrecoder. A and then include it in
Then add a reference to the static library in frameworks.
Then, include the header file where the library is used.
# Import <qrencoder/qrencoder. h>
Note: The image size may be adjusted when the QR code is displayed. Therefore, the system library quartzcore. framework must be included.
Finally, you can call the encoding library:
[CPP]View
Plaincopy
- Uiimage * image = [qrencoder encode: @ "http://www.baidu.com/"];
Note that no QR code is generated in the demo. Add it later.
Demo download: http://download.csdn.net/download/qingsi00/5001589