viewcontroller.m//CX QR code scan////Created by Ma C on 16/4/12.//Copyright? 2016 BJSXT. All rights reserved.//#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface Viewcontroller () <AVCaptureMetadataOutputObjectsDelegate> @end @implementation viewcontroller-(void) viewdidload {}-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{//Create capture session Avcapturesession * session = [[Avcapturesession alloc]init]; Add input device Avcapturedevice * device = [Avcapturedevice defaultdevicewithmediatype:@ "Avmediatypevideo"]; Avcapturedeviceinput * input = [avcapturedeviceinput deviceinputwithdevice:device error:nil]; [Session Addinput:input]; Add output data avcapturemetadataoutput * outputs = [[Avcapturemetadataoutput alloc]init]; [Output Setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()]; [Session Addoutput:output]; Tell the metadata type to be a two-dimensional code type//Note that the method will crash after add or the test requires a real machine a little trouble is not [output setmetadatAobjecttypes:@[avmetadataobjecttypeqrcode]]; Add Scan layer Avcapturevideopreviewlayer *layer = [Avcapturevideopreviewlayer layerwithsession:session]; Layer.frame = CGRectMake (10,20, Self.view.frame.size.width, 400); [Self.view.layer Addsublayer:layer]; Start scanning [session startrunning]; The following method is appropriate when the operation//stop scanning//[session stoprunning]; Remove layer//[layer Removefromsuperlayer];} -(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection{//metadataobjects for scanned data avmetadatamachinereadablecodeobject * OBJC = [metadataobjects lastobject]; We want the result NSLog (@ "%@", Objc.stringvalue);} @end
IOS QR Code scan