iOS development--advanced--production and reading of two-dimensional code

Source: Internet
Author: User


One or two-dimensional code generation

The generation and reading of two-dimensional code has been integrated from iOS7
Previously widely used ZBARSDK does not currently support 64-bit processors

Steps to generate a QR code:
Import Coreimage Framework
Generate two-dimensional code with filter Cifilter

The content of the QR code (the traditional barcode can only put the number):
Plain text
Card
Url

//1. Instantiate two-dimensional code filtersCifilter *filter = [Cifilter filterwithname:@"Ciqrcodegenerator"];//2. Restore default properties of filters[Filter setdefaults];//3. Convert a string to NSDataNSData *data =[str datausingencoding:nsutf8stringencoding];//4. Set filter inputmessage data via KVO[Filter Setvalue:data Forkey:@"InputMessage"];//5. Get the image of the filter outputCiimage *outputimage =[Filter outputimage];//6. Convert the Ciimage to uiimage and enlarge the displayreturn[UIImage imagewithciimage:outputimage Scale:20.0Orientation:uiimageorientationup];

Second, to read the two-dimensional code

Reading QR code requires importing Avfoundation frame
Use the camera to identify the contents of the QR code (simulator not available)

Input (webcam)
Convert the QR code image captured by the camera into string data
Output (data)
Show scan scene by preview layer

//1. Instantiate the shooting deviceAvcapturedevice *device =[Avcapturedevice Defaultdevicewithmediatype:avmediatypevideo];//2. Setting the input deviceAvcapturedeviceinput *input =[Avcapturedeviceinput deviceinputwithdevice:device error:nil];//3. Setting meta-data output//3.1 Instantiating the shot meta-data outputAvcapturemetadataoutput *output =[[Avcapturemetadataoutput alloc] init];//3.3 Setting the output data agent[Output Setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()];//4. Add a shooting session//4.1 Instantiating a shooting sessionAvcapturesession *session =[[Avcapturesession alloc] init];//4.2 Adding session input[Session addinput:input];//4.3 adding session output[Session addoutput:output];//4.3 Set the output data type, you need to add the meta-data output to the session before you can specify the metadata type, or you will get an error[Output SETMETADATAOBJECTTYPES:@[AVMETADATAOBJECTTYPEQRCODE]];//5. Video preview Layer//5.1 Instantiating a preview layerAvcapturevideopreviewlayer *preview =[Avcapturevideopreviewlayer layerwithsession:_session];p review.videogravity=Avlayervideogravityresizeaspectfill;preview.frame=self.view.bounds;//5.2 Inserting a layer into the current view[Self.view.layer Insertsublayer:preview Atindex: -];self.previewlayer=Preview;//6. Start the session[_session startrunning];

iOS development--advanced--production and reading of two-dimensional 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.