ISO7 Two dimensional code detailed introduction and use Method _ios

Source: Internet
Author: User

Use the system to generate/scan two-dimensional code with its own

IOS7 started the Apple integrated two-dimensional code of the generated scan ### to generate a two-dimensional code of the steps

Import coreimage Framework #import <CoreImage/CoreImage.h>

Generate two-dimensional code ### with filter Cifilte (traditional barcode can only put numbers)

Plain text

Card

Url

Generate a two-dimensional code

 
1. Create filter Cifilter *filter = [Cifilter filterwithname:@ "Ciqrcodegenerator"];

  2. Restore default [filter setdefaults];
  3. Add data to the filter (regular expression/account number and password) nsstring *datastring = @ "http://www.520it.com";
  NSData *data = [datastring datausingencoding:nsutf8stringencoding];

  [Filter Setvalue:data forkeypath:@ "InputMessage"];

  4. Get the output of two-dimensional code ciimage *outputimage = [Filter outputimage]; Because the generated two-dimensional code blur, so through the createnoninterpolateduiimageformciimage:outputimage to get the HD two-dimensional code picture//5. Display two-dimensional code self.imageView.image = [
 Self createnoninterpolateduiimageformciimage:outputimage withsize:300]; /** * Generates the specified size uiimage * * @param image Ciimage * Based on Ciimage * @param size picture Width * * (uiimage *) Createnoninterpolateduiimag
  Eformciimage: (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];
 

 

 }

scans two-dimensional code

//1. Create 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 Scan layer Avcapturevideopreviewlayer *layer = [Avcapturevideopreviewlayer layerwithsession:session];
  Layer.frame = Self.view.bounds;
  [Self.view.layer Addsublayer:layer];

  Self.layer = layer;
 

 

5. Start scanning [session startrunning]; 

The method that the scan will call

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, the last time the latest scan of the data
    avmetadatamachinereadablecodeobject *object = [metadataobjects lastobject];
    NSLog (@ "%@", object.stringvalue);

    Stop scanning
    [self.session stoprunning];

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



The above is the iOS two-dimensional code data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.