Two-dimensional code scanning (ZBARSDK) and generation (Libqrencode) capabilities on the iOS side

Source: Internet
Author: User

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
  1. id<nsfastenumeration> results = [info objectforkey:zbarreadercontrollerresults];
  2. Zbarsymbol *symbol = nil;
  3. For (symbol in results)
  4. Break ;
  5. [Self Dismissviewcontrolleranimated:yes
  6. completion:^{
  7. }];
  8. NSString *code = [NSString stringWithString:symbol.data];

⑧ Implement button function to start scanning

[CPP] View plain copy
  1. Zbarreaderviewcontroller *reader = [Zbarreaderviewcontroller new];
  2. Reader.readerdelegate = self;
  3. Reader.supportedorientationsmask = Zbarorientationmaskall;
  4. Zbarimagescanner *scanner = Reader.scanner;
  5. [Scanner setsymbology:zbar_i25
  6. Config:zbar_cfg_enable
  7. TO:0];
  8. [Self Presentviewcontroller:reader
  9. Animated:yes
  10. completion:^{
  11. }];

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
    1. 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
  1. For (UIView *temp in [Reader.view Subviews]) {
  2. For (Uitoolbar *toolbar in [temp subviews]) {
  3. if ([Toolbar Iskindofclass:[uitoolbar class]])
  4. {
  5. Uibarbuttonitem *item1 = [[Uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemcancel Target: Self action: @selector (Dismissoverlayview:)];
  6. Uibarbuttonitem *item2 = [[Uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace Target:nil Action:nil];
  7. Uibarbuttonitem *item3 = [[Uibarbuttonitem alloc]initwithtitle:@"album" Style:uibarbuttonitemstylebordered Target:  Self action: @selector (Clickphototoolbaritem:)];
  8. Nsarray *array = [Nsarray arraywithobjects:item1,item2,item3,nil];
  9. [Toolbar Setitems:array Animated:yes];
  10. }
  11. }
  12. }
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
  1. Cancel Button method
  2. -(void) Dismissoverlayview: (ID) sender{
  3. [Self Dismissviewcontrolleranimated:yes
  4. completion:^{
  5. }];
  6. }
  7. Select albums
  8. -(void) Clickphototoolbaritem: (ID) Sender
  9. {
  10. if ([Uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary])
  11. {
  12. Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller alloc] init];
  13. Imagepicker.delegate = self;
  14. imagepicker.allowsediting = NO;
  15. Imagepicker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;
  16. [Self Dismissviewcontrolleranimated:no completion:^{}];
  17. [Self Presentviewcontroller:imagepicker Animated:yes completion:^{}];
  18. }
  19. }

Two-dimensional code scanning (ZBARSDK) and generation (Libqrencode) capabilities on the iOS side

Related Article

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.