"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