How to generate two-dimensional code in iOS

Source: Internet
Author: User

First import the framework

Then generate the QR code with the filter Cifiter

#import <CoreImage/CoreImage.h>

/**

* Generate two-dimensional code

*/

-(void) creat

{

1. Create Filter objects

Cifilter *filter = [Cifilter filterwithname:@ "Ciqrcodegenerator"];

2. Setting related properties

[Filter SetDefaults];

3. Setting the input data

NSString *inputdata = @ "http://www.baidu.com";

NSData *data = [Inputdata datausingencoding:nsutf8stringencoding];

[Filter Setvalue:data forkeypath:@ "InputMessage"];

4. Get output results

Ciimage *outputimage = [Filter outputimage];

5. Display two-dimensional code

Self.imageView.image = [UIImage imagewithciimage:outputimage];

Self.imageView.image = [UIImage createnoninterpolateduiimageformciimage:outputimage withsize:200];

}

#import <AVFoundation/AVFoundation.h>

@property (nonatomic, weak) avcapturesession *session;

@property (nonatomic, weak) Avcapturevideopreviewlayer *layer;

/**

* Scan QR Code

*/

-(void) scan

{

1. Create a capture session

Avcapturesession *session = [[Avcapturesession alloc] init];

2. Add input device (data from camera input)

Avcapturedevice *device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];

Avcapturedeviceinput *input = [Avcapturedeviceinput deviceinputwithdevice:device error:nil];

[Session Addinput:input];

3. Add Output data (example Object----class object-------the Meta-class object)

Avcapturemetadataoutput *output = [[Avcapturemetadataoutput alloc] init];

[Output Setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()];

[Session Addoutput:output];

3.1. Set the type of the input metadata (type is the QR code data)

[Output SETMETADATAOBJECTTYPES:@[AVMETADATAOBJECTTYPEQRCODE]];

4. Add a scan layer

Avcapturevideopreviewlayer *layer = [Avcapturevideopreviewlayer layerwithsession:session];

Layer.frame = Self.view.bounds;

[Self.view.layer Addsublayer:layer];

Self.layer = layer;

5. Start scanning

[Session startrunning];

}

#pragma mark-callback method to implement output

This method is executed when the data is scanned

-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection

{

if (Metadataobjects.count > 0) {

Avmetadatamachinereadablecodeobject *object = [Metadataobjects lastobject];

NSLog (@ "%@", Object.stringvalue);

Stop scanning

[Self.session stoprunning];

To remove a preview layer

[Self.layer Removefromsuperlayer];

} else {

NSLog (@ "no data scanned");

}

}

How to generate two-dimensional code in iOS

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.