(105) generation and scanning of two-dimensional code

Source: Internet
Author: User

"Generate QR Code"

IOS7 after the Apple has coreimage framework for generating two-dimensional code, two-dimensional code through the filter generation, two-dimensional code filter can convert the string into a QR code.

Note that the string must be converted to nsdata incoming, and the filter is told through KVC.

Filter output to Ciimage, note to convert to uiimage use.

The code is as follows:

    1. Instantiate two-dimensional code filter    cifilter *filter = [Cifilter filterwithname:@ "Ciqrcodegenerator"];    2. Reset Filter Properties    [Filter setdefaults];    3. String goto nsdata    NSData *data = [@ "http://www.soulghost.com" datausingencoding:nsutf8stringencoding];    4. Set properties via KVC    [filter setvalue:data forkey:@ "InputMessage"];    5. Generate two-dimensional code    ciimage *qrimage = [Filter outputimage];    UIImage *image = [UIImage imagewithciimage:qrimage];


"Scan QR Code"

Scan QR code through input source camera, session conversion data, output into a string to complete the two-dimensional code parsing.

The code is as follows:

viewcontroller.m//read QR code////Created by one on 8/3/15.//Copyright (c) soulghost. All rights reserved.//#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface Viewcontroller () <AVCaptureMetadataOutputObjectsDelegate> @property (nonatomic, strong) Avcapturesession * Session, @property (Nonatomic, strong) Avcapturevideopreviewlayer *previewlayer; @end @implementation viewcontroller-(        void) viewdidload{[Super Viewdidload];    Camera as input source avcapturedevice *device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];    Avcapturedeviceinput *input = [Avcapturedeviceinput deviceinputwithdevice:device error:nil];    The output of the camera is converted to a string as the output source avcapturemetadataoutput *output = [[Avcapturemetadataoutput alloc] init];        [Output Setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()];    Instantiates a session that converts a camera-acquired image (input) into a string (output).    Avcapturesession *session = [[Avcapturesession alloc] init]; [Session Addinput:input];   [Session Addoutput:output];    _session = session;        Set output type to two-dimensional code [output SETMETADATAOBJECTTYPES:@[AVMETADATAOBJECTTYPEQRCODE]];    Create a scan view of the QR code to determine what is displayed through the session.    Avcapturevideopreviewlayer *preview = [Avcapturevideopreviewlayer layerwithsession:_session];    preview.videogravity = Avlayervideogravityresizeaspectfill;    Preview.frame = Self.view.bounds;    [Self.view.layer Insertsublayer:preview atindex:100];        _previewlayer = preview;    Open session [_session startrunning]; }-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects    Fromconnection: (avcaptureconnection *) connection{//Stop session, remove Scan view [_session stoprunning];        [_previewlayer Removefromsuperlayer]; Remove data if (Metadataobjects.count > 0) {avmetadatamachinereadablecodeobject *obj = [Metadataobjects firstObj        ECT];    NSLog (@ "%@", obj); }} @end

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

(105) generation and scanning 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.