Two-dimensional code

Source: Internet
Author: User

use System to generate/scan QR code

    • IOS7 start Apple integrated QR code generation Scan # # # # # # # # # # # # # # # #

    • Import Coreimage Frame #import <CoreImage/CoreImage.h>

    • Generate two-dimensional code with filter cifilte content (traditional barcode can only put numbers)

    • Plain text

    • Card

    • Url

generate two-dimensional code

//1. Creating FiltersCifilter *filter = [Cifilter filterwithname:@"Ciqrcodegenerator"]; //2. Restore Default[Filter setdefaults]; //3. Add data to the filter (regular expressions/accounts and Passwords)NSString *datastring =@"http://www.520it.com"; NSData*data =[datastring datausingencoding:nsutf8stringencoding]; [Filter setvalue:data Forkeypath:@"InputMessage"]; //4. Get the output of the QR codeCiimage *outputimage =[Filter outputimage]; //because the generated two-dimensional code is blurred, so through the createnoninterpolateduiimageformciimage:outputimage to obtain high-definition QR code image//5. Display two-dimensional codeSelf.imageView.image = [Self createnoninterpolateduiimageformciimage:outputimage withsize: -]; /** * ciimage generate a specified size UIImage * * @param image Ciimage * @param size picture width*/-(UIImage *) Createnoninterpolateduiimageformciimage: (Ciimage *) Image withsize: (cgfloat) size{cgrect extent=cgrectintegral (image.extent); CGFloat Scale= MIN (Size/cgrectgetwidth (extent), size/Cgrectgetheight (extent)); //1. Create bitmap;size_t width = cgrectgetwidth (extent) *Scale ; size_t Height= Cgrectgetheight (Extent) *Scale ; CGCOLORSPACEREF CS=Cgcolorspacecreatedevicegray (); Cgcontextref Bitmapref= Cgbitmapcontextcreate (nil, width, height,8,0, CS, (Cgbitmapinfo) kcgimagealphanone); Cicontext*context =[Cicontext Contextwithoptions:nil]; Cgimageref BitmapImage=[Context Createcgimage:image fromrect:extent];    Cgcontextsetinterpolationquality (Bitmapref, Kcginterpolationnone);    CGCONTEXTSCALECTM (bitmapref, scale, scale);    Cgcontextdrawimage (Bitmapref, extent, bitmapImage); //2. Save Bitmap to PictureCgimageref Scaledimage =cgbitmapcontextcreateimage (BITMAPREF);    Cgcontextrelease (BITMAPREF);    Cgimagerelease (BitmapImage); return[UIImage imagewithcgimage:scaledimage];}

Scan Two-dimensional code

//1. Create a capture sessionAvcapturesession *session =[[Avcapturesession alloc] init]; Self.session=session; //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 layerAvcapturevideopreviewlayer *layer =[Avcapturevideopreviewlayer layerwithsession:session]; Layer.frame=Self.view.bounds;    [Self.view.layer Addsublayer:layer]; Self.layer=layer; //5. Start scanning[Session startrunning];

Methods that are called by the scan

//This method is executed when the data is scanned- (void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects fromConnection :(Avcaptureconnection *) connection{if(Metadataobjects.count >0) {        //get scanned data, last time the latest scanned dataAvmetadatamachinereadablecodeobject *Object=[Metadataobjects Lastobject]; NSLog (@"%@",Object. StringValue); //Stop Scanning[Self.session stoprunning]; //to remove a preview layer[Self.layer Removefromsuperlayer]; } Else{NSLog (@"no scan to data"); }}

Two-dimensional code

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.