Now the two-dimensional code everywhere, whether it is physical goods or a variety of vouchers are the two-dimensional code of the figure. Mobile devices, such as mobile phones, have become a good application platform for QR codes, and we have basically used the third-party class "ZBarSDK" before iOS7, but after iOS7 Avmetadataobject provides us with an interface for parsing two-dimensional code, because its own research on this piece is not deep, after finding the data found that the use of native API scanning and processing efficiency is much higher than the third-party library, So here's a brief introduction to the official OST API QR code and barcode scanning
Take a look at the following code:
First introduce the header file, set up a good agent
#import " LBViewController.h " #import <AVFoundation/AVFoundation.h>@interface lbviewcontroller () < Avcapturemetadataoutputobjectsdelegate>// set Agent to process the image after scanning the information { *session; Scan input/output intermediate bridge }
- (void) viewdidload{[Super Viewdidload]; //Get camera EquipmentAvcapturedevice *device =[Avcapturedevice Defaultdevicewithmediatype:avmediatypevideo]; //Create an Input objectAvcapturedeviceinput *inputdevice =[Avcapturedeviceinput Deviceinputwithdevice:device Error:nil]; //creating an Output objectAvcapturemetadataoutput *outdevice =[[Avcapturemetadataoutput alloc] init]; //here is a limit to the range of scans we see (the numbers below represent the bottom half of the interface) (X,Y,W,H) are judged by the points in the upper left corner.Outdevice.rectofinterest = CGRectMake (0.5,0,0.5,1); //set the agent to refresh in the main thread[Outdevice setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()]; //Initializing linked objectsSession =[[Avcapturesession alloc] init]; //set the quality of the collected data[Session Setsessionpreset:avcapturesessionpresethigh];//High quality[Session Addinputwithnoconnections:inputdevice]; [Session Addoutputwithnoconnections:outdevice]; //set the encoding format supported by the scan code (as follows: Set the barcode and QR code compatible)Outdevice.metadataobjecttypes =@[avmetadataobjecttypeqrcode,avmetadataobjecttypeean13code]; Avcapturevideopreviewlayer*layer =[Avcapturevideopreviewlayer layerwithsession:session]; Layer.videogravity=Avlayervideogravityresizeaspectfill; Layer.frame=Self.view.layer.bounds; [Self.view.layer insertsublayer:layer Atindex:0]; //Start capturing data[Session startrunning];}
Here is the callback for the proxy
-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection{ if (Metadataobjects.count > 0) { Avmetadatamachinereadablecodeobject *metadaobject = [metadataobjects objectatindex:0]; Output scanned string NSLog (@ "%@", Metadaobject.stringvalue);} }
Disclaimer: I am only a beginner, if there is a problem, welcome friends to correct, discuss together. Thank you!
ios-Two-dimensional code scanning