IOS note 061 two-dimensional code generation and scanning _ios

Source: Internet
Author: User

Now two-dimensional code everywhere, whether it is a kind of goods or a variety of gift certificates are not the figure of two-dimensional code. Mobile devices such as mobile phones and become a two-dimensional code of a good application platform, whether it is to generate two-dimensional code or sweep code two-dimensional code. This article from the generation of two-dimensional code, scanning two-dimensional code to expand the analysis, through the content of the two-dimensional code is easy to use.

First of all, the generation of two-dimensional code

Two-dimensional code can be stored in plain text, business cards or URLs

Second, the steps to generate two-dimensional code:

Import Coreimage Framework

To generate a two-dimensional code again through the filter cifilter

1. Create filter

2, restore the filter's default properties

3, set the content

4. Get output file

5, display two-dimensional code

Code implementation Coreimage

 Two-dimensional code generation
 //1, create filter
 cifilter *filter = [Cifilter filterwithname:@ "Ciqrcodegenerator"];
 2, restore the filter's default properties
 [Filter setdefaults];
 3, set content
 nsstring *str = @ "This is a two-dimensional code generation result";
 NSData *data = [str datausingencoding:nsutf8stringencoding];
 Use KVO to set properties
 [Filter Setvalue:data forkey:@ "InputMessage"];
 4, get output file
 ciimage *outputimage = [Filter outputimage];
 5, display two-dimensional code
 self.imageView.image = [UIImage imagewithciimage:outputimage];

The picture shown is not very clear, you can redraw the picture yourself

Re-build HD pictures: Online Search can be, the specific process can not care for the moment

/** * Generates the specified size uiimage * * @param image Ciimage * Based on Ciimage * @param size picture Width * * (uiimage *) Createnoninterpolateduiima
 Geformciimage: (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 picture cgimageref Scaledimage = Cgbitmapcontextcreateimage (bitmapref);
 Cgcontextrelease (BITMAPREF); CgImagerelease (BitmapImage);
return [UIImage Imagewithcgimage:scaledimage]; }

There is the setting of content for the URL, if with the protocol header, will automatically open the Web page.

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

Must have a protocol header to open

Back to the top of

Scan two-dimensional code

Avfoundation Framework

The scanning process of two-dimensional code

1. Create capture session Avcapturesession

Add input device (data from camera input) Avcapturedevice Avcapturedeviceinput

2, add output data (Sample Object--> class object--> object--> root class object) Avcapturemetadataoutput

2.1. Set the type of input metadata (type is two-dimensional code data) setmetadataobjecttypes

3. Add Scan layer Avcapturevideopreviewlayer

4. Start Scanning startrunning

5, implement callback proxy method, get scan result captureoutput::

#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface Viewcontroller () < avcapturemetadataoutputobjectsdelegate>/** Display Layer */@property (nonatomic, strong) Avcapturevideopreviewlayer *layer
;
/** Capture Session * * @property (nonatomic, strong) avcapturesession *session; @end @implementation Viewcontroller-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event {//two D code Scan//1, Chuang
 Build Capture Session Avcapturesession *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 (Sample Object--> class object--> object--> root class object) avcapturemetadataoutput *output = [[Avcapturemetadataoutput alloc]
 INIT];
 [Output Setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()];
 [Session Addoutput:output]; 3.1. Set the type of input metadata (type is two-dimensional code data) [OUTput Setmetadataobjecttypes:@[avmetadataobjecttypeqrcode]];
 4, add scanning layer avcapturevideopreviewlayer *layer = [Avcapturevideopreviewlayer layerwithsession:session];
 Layer.frame = Self.view.bounds;
 [Self.view.layer Addsublayer:layer];
 Self.layer = layer;
5, start scanning [session startrunning]; /** * Implement output callback method/-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) met Adataobjects fromconnection: (avcaptureconnection *) connection {//array metadataobjects holds the result data if (metadataobjects.count
 > 0) {//Get final read result avmetadatamachinereadablecodeobject *object = [Metadataobjects lastobject];
 NSLog (@ "%@", Object.stringvalue);
 [Self.session stoprunning];
 [Self.layer Removefromsuperlayer];
 else {NSLog (@ "No scan to data"); }
}

Summed up one sentence: This two-dimensional code is not difficult to use

This is the end of this article, iOS notes 061 two-dimensional code generation and scanning hope that in the future work and learning can help everyone. The following article gives you some information on how to use a two-dimensional code on an Apple iOS device, and you need to know the friend click here.

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.