Now the two-dimensional code everywhere, whether it is physical goods or a variety of vouchers are the two-dimensional code of the figure. The two-dimensional code used in mobile phones is also very wide, such as. Just recently collected and summed up the use of the next two-dimensional code
Here's how to use the QR code on your iOS device
First download the Zbar SDK on GitHub
Address Https://github.com/bmorton/ZBarSDK
Then add the following related class libraries to the
Avfoundation.framwork, Coremedia.framework, Corevideo.framework, Libiconv.dylib and LIBZBAR.A
The next step is to refer to the. h file First
#import "ZBarSDK.h"
Add agents at the same time
Zbarreaderdelegate
1. Scan QR code (including read and decode)
Scanning the two-dimensional code of open Source Library has many such as Zbar, zxing and so on. Bo Master first tried zxing, vertical screen of the case zxing no problem, but it seems that horizontal screen will appear dislocation situation. So Bo Master abandoned zxing, use Zbar.
Here to mention, zxing of open source do better than Zbar, reading and decoding speed is also fast, but integration into their own projects is to see the pain, not only to import the entire demo project, Setup is relatively cumbersome, if not really need ultra-high-performance application developers, bloggers still recommend the use of Zbar.
The overall process is as follows:
① download ZBARSDK, contains ZBARSDK header package, libzbar.a static link library file and resource file three parts;
② the following frameworks into the new project: Avfoundation.framework, Coremedia.framework, Corevideo.framework, Quartzcore.framework, Libiconv.dylib (there is no need to rejoin);
③ will zbarsdk join the project;
④ Check LIBZBAR.A link library is normal, if there is no automatic link, need to join manually, such as:
⑤ Reference header file #import "ZBarSDK.h" in the page. h file that you need to use
⑥ the page you want to use. Inheritance <ZBarReaderDelegate> Agreement in H
⑦ method to implement the Protocol in. M-(void) Imagepickercontroller: (uiimagepickercontroller*) Reader Didfinishpickingmediawithinfo: ( nsdictionary*) Info
[CPP] View plain copy
- id<nsfastenumeration> results = [info objectforkey:zbarreadercontrollerresults];
- Zbarsymbol *symbol = nil;
- For (symbol in results)
- Break ;
- [Self Dismissviewcontrolleranimated:yes
- completion:^{
- }];
- NSString *code = [NSString stringWithString:symbol.data];
⑧ Implement button function to start scanning
[CPP] View plain copy
- Zbarreaderviewcontroller *reader = [Zbarreaderviewcontroller new];
- Reader.readerdelegate = self;
- Reader.supportedorientationsmask = Zbarorientationmaskall;
- Zbarimagescanner *scanner = Reader.scanner;
- [Scanner setsymbology:zbar_i25
- Config:zbar_cfg_enable
- TO:0];
- [Self Presentviewcontroller:reader
- Animated:yes
- completion:^{
- }];
2. Generate two-dimensional code
In the generation of two-dimensional code of the library Qrencoder most common, but because of the particularity of Chinese characters, when generating a certain error, so Bo master instead of Libqrencode, is a pure C-written class library, support surface is also more extensive.
① download Libqrencode source code;
② the following frameworks into the new project: Avfoundation.framework, Coremedia.framework, Corevideo.framework, Quartzcore.framework, Libiconv.dylib (there is no need to rejoin);
③ will libqrencode source code to join the project;
④ the page to use. m file referencing header file #import "QRCodeGenerator.h";
⑤ realization of generating two-dimensional code images
[CPP] View plain copy
- UIImage *image = [Qrcodegenerator qrImageForString:self.codeString imageSize:self.imageView_Code.bounds.size.width] ;
3. Customizing the Scan page
Zxing supports its own creation of scanned pages, and because Zbar uses the. A link library file, it needs to modify his generated scan page and not create a new one directly.
In the above scan QR Code section ⑧, some changes to reader, the code is as follows:
[CPP] View plain copy
- For (UIView *temp in [Reader.view Subviews]) {
- For (Uitoolbar *toolbar in [temp subviews]) {
- if ([Toolbar Iskindofclass:[uitoolbar class]])
- {
- Uibarbuttonitem *item1 = [[Uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemcancel Target: Self action: @selector (Dismissoverlayview:)];
- Uibarbuttonitem *item2 = [[Uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace Target:nil Action:nil];
- Uibarbuttonitem *item3 = [[Uibarbuttonitem alloc]initwithtitle:@"album" Style:uibarbuttonitemstylebordered Target: Self action: @selector (Clickphototoolbaritem:)];
- Nsarray *array = [Nsarray arraywithobjects:item1,item2,item3,nil];
- [Toolbar Setitems:array Animated:yes];
- }
- }
- }
In the code above, the Cancel button and the album button method need to be defined by themselves. The detailed code in the method is as follows: [CPP] View plain copy
- Cancel Button method
- -(void) Dismissoverlayview: (ID) sender{
- [Self Dismissviewcontrolleranimated:yes
- completion:^{
- }];
- }
- Select albums
- -(void) Clickphototoolbaritem: (ID) Sender
- {
- if ([Uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary])
- {
- Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller alloc] init];
- Imagepicker.delegate = self;
- imagepicker.allowsediting = NO;
- Imagepicker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;
- [Self Dismissviewcontrolleranimated:no completion:^{}];
- [Self Presentviewcontroller:imagepicker Animated:yes completion:^{}];
- }
- }
Two-dimensional code scanning (ZBARSDK) and generation (Libqrencode) capabilities on the iOS side