QR Code barcode scanning using IOS7 native API
prior to IOS7, developers typically use third-party libraries for code-scanning programming. commonly used is the ZBarsdk,ios7 after , the system's Avmetadataobject class, we provide an interface for parsing two-dimensional code. Tested, the use of native API scanning and processing is very efficient, far higher than the third-party library.
I. Examples of usage methods
The official interface is very simple and the code is as follows:
@interface ViewController () <avcapturemetadataoutputobjectsdelegate>//agent { for processing collected information avcapturesession * session;//Intermediate bridge for input and output} @end @implementation viewcontroller- ( void) Viewdidload { [super viewdidload]; // do any additional setup after loading the view, typically from a nib. //Acquiring camera Equipment avcapturedevice * device = [avcapturedevice defaultdevicewithmediatype:avmediatypevideo]; // Create an input stream AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceinputwithdevice:device error:nil]; //creating an output stream avcapturemetadataoutput * output = [[avcapturemetadataoutput alloc]init]; //set proxy refresh &nbs in main threadP; [output setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()]; //initializing a linked object session = [[avcapturesession alloc]init]; //High Quality acquisition rate [session setsessionpreset: avcapturesessionpresethigh]; [session addinput:input]; [session addoutput:output]; //set the encoding format supported by the scan Code (barcode and QR Code are compatible) [email protected][AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, avmetadataobjecttypeean8code, avmetadataobjecttypecode128code]; avcapturevideopreviewlayer * layer = [avcapturevideopreviewlayer layerwithsession:session]; layer.videogravity=avlayervideogravityresizeaspectfill ; layer.frame=self.view.layer.bounds; [self.view.layer insertsublayer:layer atindex:0]; //start capturing [session startrunning];}
After that, we can see what the camera captures on our UI, and as long as we implement the methods in the proxy, we can scan the barcode of the QR code:
-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection{if (metadataobjects.count>0) {//[session stopRunning]; Avmetadatamachinereadablecodeobject * MetadataObject = [metadataobjects objectatindex:0]; Output Scan string NSLog (@ "%@", Metadataobject.stringvalue); }}
second, some optimization
Through the above code test, we can find that the system's resolution processing efficiency is quite high, iOS official API is also really very powerful, however, we can do further optimization, will be more efficient:
First ,There is one such attribute in the Avcapturemetadataoutput class (available after IOS7.0):
@property(nonatomic) cgrect rectofinterest;
This property is roughly meant to tell the system what it needs to be aware of, and most apps have a box in the sweep code UI that reminds you to place the barcode in that area, where it can set a range to handle only the information captured within that range. As a result, we can imagine that the efficiency of our code will be greatly improved, when using this property. A few points to note:
1, this cgrect parameter and the normal rect range is not quite the same, its four values range is 0-1, representing the scale.
2. After testing, the x in this parameter corresponds exactly to the vertical distance from the upper left corner, and y corresponds to the horizontal distance from the upper left corner.
3, Width and height settings are similar.
3. For example, if we want the scanned processing area to be the lower half of the screen, we set
Output.rectofinterest=cgrectmake (0.5,0,0.5, 1);
Why do Apple design this, or is this parameter my usage there is not right, but also need to know the friend to give a guide.
Where the omission is welcome
Learn to use welcome reprint
IOS7 QR Code and barcode scanning using native API