ios-Two-dimensional code scanning

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.