Cat Share, must boutique
Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243
The previous article wrote how to generate two-dimensional code, here say how to read it, anyway is very simple, iOS package is too powerful
The step is this:
Reading QR code requires importing Avfoundation frame #import <AVFoundation/AVFoundation.h>
1: Use the camera to identify the contents of the QR code (simulator not).
2: Input (camera).
3: By the session will be captured by the camera QR code image into string data.
4: Output (data).
5: The scan scene is displayed by the preview layer.
#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 {[SuperViewdidload];additional setup after loading the view, typically from a nib. //1. Instance shooting deviceAvcapturedevice *device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];//2. Setting the input deviceAvcapturedeviceinput *input = [avcapturedeviceinput deviceinputwithdevice:device error:Nil];//3. Setting meta-data output //3.1 instantiation of the shot meta-data outputAvcapturemetadataoutput *output = [[Avcapturemetadataoutput alloc] init];//3.3 Set output data proxy[Output setmetadataobjectsdelegate: SelfQueue:dispatch_get_main_queue ()];//4. Add a shooting session //4.1 instancing of shooting sessionsAvcapturesession *session = [[Avcapturesession alloc] init];//4.2 Add session Input[Session Addinput:input];//4.3 add session output[Session Addoutput:output];///4.3 Set the output data type, you need to add the meta-data output to the session before you can specify the metadata type, or you will get an error[Output SETMETADATAOBJECTTYPES:@[AVMETADATAOBJECTTYPEQRCODE]]; Self. Session= Session;//5. Video Preview Layer //5.1 Instantiate a preview layer, passing _session to tell the layer what to display in the futureAvcapturevideopreviewlayer *preview = [Avcapturevideopreviewlayer layerwithsession:_session]; Preview. Videogravity= Avlayervideogravityresizeaspectfill; Preview. Frame= Self. View. Bounds;//5.2 Inserting a layer into the current view[ Self. View. LayerInsertsublayer:preview Atindex: -]; Self. Previewlayer= Preview;//6. Start the session[_session startrunning];}#pragma mark-avcapturemetadataoutputobjectsdelegate Proxy method- (void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray*) metadataobjects fromconnection: (avcaptureconnection *) connection{//frequent scan, invoke proxy method //1. If the scan is complete, stop the session[ Self. SessionStoprunning];//2. Delete the preview layer[ Self. PreviewlayerRemovefromsuperlayer];NSLog(@"%@", metadataobjects);//3. Set interface to display scan results if(metadataobjects. Count>0) {Avmetadatamachinereadablecodeobject *obj = metadataobjects[0];//Tip: If you need to scan for information such as URLs or business cards, you can expand here! //_label.text = Obj.stringvalue; NSLog(@"%@"Obj. StringValue); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Cat Learn the QR code of iOS, quickly open the camera to read the QR code