IOS Two-dimensional code

Source: Internet
Author: User

Basically we do two-dimensional code implementation method has three main ways: Zbar, ZXing, Avfoundation.

The first type: Zbar

1. Download zbarsdk and import into the project

2. Import Dependent libraries:

quartzcore.framework, Corevideo.framework, Coremedia.framework,avfoundation.framework

3. If does not contain bitcode, please click here

There are two ways of 4.Zbar:

The 1> mode is a regular way to scan through the Zbarreaderviewcontroller directly by opening a scanning interface.

First refer to the header file #import "ZBarSDK.h" to implement proxy <ZBarReaderDelegate>

Initialize scan QR Code controller

Zbarreaderviewcontroller *reader = [Zbarreaderviewcontroller new];

Set up Proxy

Reader.readerdelegate = self;

QR Code/Barcode Recognition Settings

Zbarimagescanner *scanner = Reader.scanner;

[Scanner setsymbology:zbar_i25

Config:zbar_cfg_enable

TO:0];

Pop-up system camera, full screen shot

[Self Presentviewcontroller:reader animated:yes completion:nil];

-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info

{

id<nsfastenumeration> results = [info objectforkey:zbarreadercontrollerresults];

Zbarsymbol * symbol;

for (symbol in results)

Break

Self.imageView.image = [info objectforkey:uiimagepickercontrolleroriginalimage];

[Picker Dismissviewcontrolleranimated:yes Completion:nil];

Self.label.text = Symbol.data;

Note: Symbol.data is the value after scanning the QR code, [info objectforkey:uiimagepickercontrolleroriginalimage] is the image after scanning the QR code

}

2> mode is the way to customize the scanning interface. That is, customize the Zbarreaderview interface to achieve customized results.

First refer to the header file #import "ZBarSDK.h" to implement proxy <ZBarReaderViewDelegate>

Initialize scan QR code object Zbarreaderview

_readview = [[Zbarreaderview alloc] init];

Set up a scan agent

_readview.readerdelegate = self;

Turn off the Flash

_readview.torchmode = 0;

Show frame rate

_readview.showsfps = NO;

Add its camera capture view to the view you want to display

[Self.view Addsubview:_readview];

QR Code/Barcode Recognition Settings

Zbarimagescanner *scanner = _readview.scanner;

[Scanner setsymbology:zbar_i25

Config:zbar_cfg_enable

TO:0];

When scanning the QR code, the recognition success will enter this method, read the QR code content

-(void) Readerview: (zbarreaderview*) Readerview didreadsymbols: (zbarsymbolset*) symbols FromImage: (uiimage*) image

{

Zbarsymbol * Symbol =nil;

for (symbol in symbols)

Break

Note: Symbol.data is the value after scanning the QR code, image is the image after scanning the QR code

}

When generating two-dimensional code:

First, import the Libqrencode framework to #import "QRCodeGenerator.h" guide in

The returned image = [Qrcodegenerator qrimageforstring:@ ' string "imageSize:testImageView.bounds.size.width];

Of course, you can save it to an album

Save QR code images to albums

-(void) saveimagetoalbum

{

Uiimagewritetosavedphotosalbum (The returned image, self, @selector (image:didFinishSavingWithError:contextInfo:), NULL);

}

Save Picture callback method

-(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) ContextInfo

{

if (Error! = NULL)

{

Uialertcontroller *ALERTVC = [Uialertcontroller alertcontrollerwithtitle:@ "Please open the app's album permissions" Message:nil PreferredStyle: Uialertcontrollerstylealert];

Uialertaction *action = [uialertaction actionwithtitle:@ "OK" style:uialertactionstylecancel handler:^ (UIAlertAction * Action) {

}];

[ALERTVC addaction:action];

[Self PRESENTVIEWCONTROLLER:ALERTVC animated:yes completion:^{

}];

}

Else

{

Uialertcontroller *ALERTVC = [Uialertcontroller alertcontrollerwithtitle:@ "saved to album" Message:nil PreferredStyle: Uialertcontrollerstylealert];

Uialertaction *action = [uialertaction actionwithtitle:@ "OK" style:uialertactionstylecancel handler:^ (UIAlertAction * Action) {

}];

[ALERTVC addaction:action];

[Self PRESENTVIEWCONTROLLER:ALERTVC animated:yes completion:^{

}];

}

}

IOS QR code

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.