IOS generate and read two-dimensional code card _ios

Source: Internet
Author: User

I. Overview
1, through a certain geometry of a certain pattern in the plane (two-dimensional direction) distribution of Black-and-white graphics record data symbol information
2, two-dimensional code can usually contain the following content

    • Plain text
    • Card
    • Url

3, two-dimensional code has a very wide range of applications

    • Two-dimensional code card
    • Sweep Code Payment
    • Web address (URL), after scanning automatically open the URL

Two or two-D code generation
1, the principle of formation
A class Cifilter (filter) contains all the information in a two-D code and then generates a two-dimensional code picture
The icon (avatar) in the middle of the two-dimensional code is implemented by adding a picture to the generated picture
2. Build Steps

    • Create a filter
    • Initializing filters
    • Add two-dimensional code information
    • Get the generated two-dimensional code picture
    • Show the two-dimensional code picture

3, the resulting two-dimensional code of the effect of the map

4, the specific code realization
1), through storyboard to create two ImageView, respectively, to display two-dimensional code pictures and avatar

2), has the display two-dimensional code picture ImageView

@property (Weak, nonatomic) Iboutlet Uiimageview *imageview;

3), generate two-dimensional code

When/** clicks on the screen, it displays the two-dimensional code *
/-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event
{
  / /1. Create Filters
  Cifilter *filter = [Cifilter filterwithname:@ "Ciqrcodegenerator"];

  2. Restore default
  [filter setdefaults];

  3. Add data to
  the filter nsstring *datastring = @ "http://www.xxx.com/theDesertIslandOutOfTheWorld/";  nsstring *datastring = @ "secular island";
  Convert data to NSData type
  nsdata *data = [datastring datausingencoding:nsutf8stringencoding];
  The two-dimensional code input information of the filter is set through KVC
  [filter setvalue:data forkey:@ "InputMessage"];

  4. Get the output two-dimensional code picture (ciimage type)
  ciimage *outimage = [Filter outputimage];
  Replace the ciimage type of picture with a uiimage type of picture
  uiimage *image = [UIImage imagewithciimage:outimage];

  5. Show two-dimensional code picture
  self.imageView.image = image;
}

4, through two-dimensional Code scanning tool, you can scan the generated two-dimensional code
If the input information for the two-dimensional code is a URL, the specified network resource is opened
three or two-D code scan
1. Note:

    • Two-dimensional code sweep code needs to use the camera, the need for real machine equipment
    • Xcode7 is a free real-machine debugging.

2, two-dimensional code scanning process

    • Scan two-dimensional code via camera
    • Parsing two dimensional code data
    • Perform the appropriate operation based on two-dimensional code information

3. Process Analysis
1, through the camera scanning two-dimensional code information needs to use the Avcapturesession class

    • This class is used to coordinate data from the audio/video input port to the output end
    • This class requires an input device avcapturedeviceinput, usually a camera
    • This class requires an output, outputs a specified type of data, and has multiple classes to choose from (e.g. Avcapturemetadataoutput)
    • Requires the call-startrunning method to open the input source

2, data captured through the Avcapturesession class, you can parse the captured data with a specific type by specifying the filter type of the output port
3), you can set the agent for the output avcapturemetadataoutput to listen to the capture process
4), through the Avcapturevideopreviewlayer class to render the raw data obtained to the specified layer
4, two-dimensional code generation process
To save the coordinator of the input source and output by member properties (Avcapturesession object)

@property (nonatomic, weak) avcapturesession *session;

Save a layer with member properties that shows the metadata entered by the input device to remove the layer at the appropriate time

@property (nonatomic, weak) Avcapturevideopreviewlayer *layer;

Create an object for the Avcapturesession class (short name: Capture object) to capture data for the audio/video port

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

Add an input device to the capture object (usually a camera)

Sets the type of input device input data (VIDEO)
avcapturedevice *device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];
Avcapturedeviceinput *input = [Avcapturedeviceinput deviceinputwithdevice:device error:nil];
[Session Addinput:input];

Add output to capture object

Avcapturemetadataoutput *output = [[Avcapturemetadataoutput alloc] init];
Set Agent
[Output setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()];
[Session Addoutput:output];

Set filter criteria for input sources (get only two-dimensional code information)

[Output SETMETADATAOBJECTTYPES:@[AVMETADATAOBJECTTYPEQRCODE]];

To display the captured raw data to the controller's view

Avcapturevideopreviewlayer *layer = [Avcapturevideopreviewlayer layerwithsession:session];
Layer.frame = self.view.bounds;
[Self.view.layer Addsublayer:layer];

Start fetching data

[Session startrunning];

Implement the proxy method of the output end, monitor the capture process

-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection
{
  if (Metadataobjects.count > 0)
  {
    //Get output information
    avmetadatamachinereadablecodeobject *object = [metadataobjects lastobject];
    NSLog (@ "%@", object.stringvalue);

    Stop scanning
    [self.session stoprunning];

    Remove the presentation layer
    [Self.layer removefromsuperlayer];
  }
  else
  {
    NSLog (@ "not scanned to data");
  }

The above is the iOS two-dimensional code card generation and read the detailed code introduction, I hope to help you learn.

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.