Two-dimensional code generation and scanning

Source: Internet
Author: User

Occasionally with this piece of things, always can't remember, so wrote two demo comments written in very detailed.

Two-dimensional code generation:

#import "ViewController.h"#import<CoreImage/CoreImage.h>@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview*Codeimage;@end/*1. Generate QR code to import coreimage this system native framework (because to get the built-in filter supports various kinds of QR code) 2. (InputMessage, linked string (input is what we set ourselves) Inputcorrectionlevel link type)  3. Remember that sweep QR code is an operation based on the type of string obtained to let him do the next operation. 4.filter.outputimage get picture 5. Adjust sharpness*/@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //1. Create filter object built-in filters support various kinds of QR codes with one type specific what format is unclear//Print all objects by printing with a categorized property//NSLog (@ "%@", [Cifilter Filternamesincategory:kcicategorybuiltin]); //Two-dimensional code filter has been obtainedCifilter *filter = [Cifilter filterwithname:@"Ciqrcodegenerator"]; //2. Set Default values[Filter setdefaults]; //3. Input stringNSLog (@"%@", Filter.inputkeys);//It 's all right. This string is considered to be a special type of string//read-only properties are replicated using KVC//ciqrcodegenerator filter requires nsdata for inputmessage this buddy must be a two-dimensional code//to convert a string into a 2 binary method[Filter setvalue:[@"http://mp.weixin.qq.com/wiki/14/9f9c82c1af308e3b14ba9b973f99a8ba.html"datausingencoding:nsutf8stringencoding] Forkey:@"InputMessage"]; NSLog (@"%@", Filter.inputkeys); //5. Enlarge image Ciimage is inherited from NSObject (reason: Ciimage is really magnified five times times, then Codeimageframe does not change, pixel dot dense becomes clear)Ciimage *resultimage = [Filter.outputimage Imagebyapplyingtransform:cgaffinetransformmakescale (5,5)]; Self.codeImage.image=[UIImage Imagewithciimage:resultimage]; //    //4. Get a picture of a two-dimensional code//ciimage *imagec = filter.outputimage;//    //self.codeImage.image = [UIImage Imagewithciimage:imagec];//    }@end

Two-dimensional code reads:

#import "ViewController.h"#import<AVFoundation/AVFoundation.h>@interfaceViewcontroller () <AVCaptureMetadataOutputObjectsDelegate>@property (nonatomic,strong) avcapturesession*session; @property (Nonatomic,strong) avcapturedeviceinput*input; @property (Nonatomic,strong) avcapturemetadataoutput*output;//Create a Preview view layer is designed to display the input device to capture the screen@property (Nonatomic,strong) Avcapturevideopreviewlayer *viewlayer;//inherit from Calayer@end/*1. Import avfoundtation frame input device Camera mic keyboard etc Avcapturedeviceinput (subclass) Avcaptureinput (parent Class) output device parsing (metadata) Avcapturemetad Ataoutput-avcaptureoutput Session link will input device and output device to establish contact QR Code of the scan code and screen display things are not related, but the camera alignment with the QR code for the later page jump and other operations is the other business logic we only     is to parse this string after the data has been obtained. Format metadata: Metadata (MetaData) because they are data/information used to describe specific data/information. In daily life, meta data is ubiquitous.     A class of things, you can define a set of meta-data.  The greatest benefit of metadata is that it enables the description and categorization of information to be formatted, thus creating the possibility for machine processing. 1. Create input device object Avcapturedeviceinput and set device type 2, create output device object Avcapturemetadataoutput (metadata object) 3. Create a Avcapturesession object to connect the input and output (add) to the 4.  Set video range [Self.session Setsessionpreset:avcapturesessionpresethigh]; 5. Turn on 6, set the Avcapturemetadataoutput data and get the object. (7. Set the type of output device (note that there is a pit)*//*output devices with data are available parameters ("Org.iso.Aztec", "org.iso.Code128", "Org.iso.Code39", "org.iso.Code39Mod43", " Com.intermec.Code93 "," Org.iso.DataMatrix "," org.gs1.ean-13 "," org.gs1.ean-8 "," Org.ansi.Interleaved2of5 "," Org.gs1.ITF14 "," org.iso.PDF417 "," Org.iso.QRCode ", two-dimensional code type" ORG.GS1.UPC-E ", face)*/@implementationViewcontroller- (void) viewdidload {[Super viewdidload];}- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{        //1 Creating an input device (type is camera)//given a device let us know which device avmediatypevideo is the string of this style?? //Meta DataAvcapturedevice *device =[Avcapturedevice Defaultdevicewithmediatype:avmediatypevideo]; Self.input=[[Avcapturedeviceinput Alloc]initwithdevice:device Error:nil]; //2, create an output device to parseSelf.output = [[Avcapturemetadataoutput alloc]init];// ?? //2,1 Set the proxy for the output device (the location of proxy settings for this place is very special)//Why is this place going to set a global queue[Self.output setmetadataobjectsdelegate:self Queue:dispatch_get_global_queue (0,0)]; //    //6. Set the metadata type difference//    //go ahead and print out what types are available (7.0 before and 7.0 after this place is there)//NSLog (@ "%@", self.output.metadataObjectTypes);//data types that metadata can take advantage of//            //3. Create a session layer to establish a connectionself.session=[[Avcapturesession alloc]init]; //3.1 First determine if you can add the device and add        if([self.session canAddInput:self.input]) {[Self.session addInput:self.input]; }    if([self.session canAddOutput:self.output]) {[Self.session addOutput:self.output]; }        //5. Setting the video range//[Self.session Setsessionpreset:avcapturesessionpresethigh]; //High is self-adapting[Self.session Setsessionpreset:avcapturesessionpresethigh]; //6. Setting the metadata type//go ahead and print out what the available types areNSLog (@"%@", self.output.availableMetadataObjectTypes);//data types that metadata can take advantage of//To set according to the type[Self.output Setmetadataobjecttypes:@[avmetadataobjecttypeqrcode]]; //4. Turn on the session[Self.session startrunning]; //8. Add a preview view to display the input device screen//CreateSelf.viewlayer =[[Avcapturevideopreviewlayer alloc]initwithsession:self.session]; //Set FrameSelf.viewLayer.frame =Self.view.bounds; //[Self.view AddSubview:self.viewLayer]; Inherit from Calayer[Self.view.layer AddSublayer:self.viewLayer]; }#pragmaMark--The agent's method of parsing the data will call this method//A string will be returned to you after the data parsing is complete- (void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects fromConnection :(Avcaptureconnection *) connection{//the scanned data is all in metadataobjects since it's an array, go ahead and print him .//NSLog (@ "%@", metadataobjects); //Take out the scanned dataAvmetadatamachinereadablecodeobject *OBJC =[Metadataobjects Firstobject]; NSLog (@"%@", Objc.stringvalue); //Close Session[Self.session stoprunning]; //remove this layer[Self.viewlayer Removefromsuperlayer];}@end

Two-dimensional code generation and scanning

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.