1, build scan QR code interface. Storyboard can be used to build
2. Create the object's controller class to manage the scan of QR code
3. Import in this class #import <AVFoundation/AVFoundation.h> Frame
4. Call in Viewdidload
1 [self setupqrcode];
5, implement the above method
- (void) setupqrcode{//1. Get input deviceAvcapturedevice *inputdevice =[Avcapturedevice Defaultdevicewithmediatype:avmediatypevideo]; //2. Create input object based on input deviceAvcapturedeviceinput *inputobj =[[Avcapturedeviceinput alloc] Initwithdevice:inputdevice Error:nil]; //3. Create an Output objectAvcapturemetadataoutput *outputobj =[[Avcapturemetadataoutput alloc] init]; //4, the creation of the output object agent, here set the agent for self, to abide by the agent protocol, implement proxy method[Outputobj setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()]; //5. Create a sessionAvcapturesession *session =[[Avcapturesession alloc] init]; Self.session= Session;//The session is a global variable, so set to: property//6. Add input and output to session if([session canaddinput:inputobj]) {[Session addinput:inputobj]; } if([session canaddoutput:outputobj]) {[Session addoutput:outputobj]; } //7, set the type of output (be sure to add the input object to the session and then set the output type, otherwise it will be an error)[Outputobj Setmetadataobjecttypes:@[avmetadataobjecttypeqrcode]];
/* This is the type to be set when sweeping the face bar code
[Outputobj setmetadataobjecttypes:
@[avmetadataobjecttypeupcecode,
Avmetadataobjecttypecode39code,
Avmetadataobjecttypecode39mod43code,
Avmetadataobjecttypeean13code,
Avmetadataobjecttypeean8code,
Avmetadataobjecttypecode93code,
Avmetadataobjecttypecode128code,
Avmetadataobjecttypepdf417code,
Avmetadataobjecttypeazteccode,
Avmetadataobjecttypeinterleaved2of5code,
Avmetadataobjecttypeitf14code,
Avmetadataobjecttypedatamatrixcode]];
*/
// 8. Setting the Preview interface of sweep surface Avcapturevideopreviewlayer *previewlayer = [Avcapturevideopreviewlayer layerwithsession:session]; = self.view.bounds; [Self.view.layer insertsublayer:previewlayer Atindex: 0 ]; // 9. Start collecting data (since scanning is a persistent process,) [Session startrunning];}
6, the implementation of the 4th step of the agent method
#pragmaMark-avcapturemetadataoutputobjectsdelegate implement this proxy method, (this agent inside this one method)-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects fromConnection :(Avcaptureconnection *) connection{//This method is called after the data is scanned. Because the call is very frequent, it is judged that when the data is captured, the scan stops immediately if(Metadataobjects.count >0) { //1. Stop Scanning[Self.session stoprunning]; //2. Remove the preview screen[Self.previewlayer Removefromsuperlayer]; //3. Take out the dataAvmetadatamachinereadablecodeobject *obj =[Metadataobjects Lastobject]; NSString*str =Obj.stringvalue; NSLog (@"%@", str);//the STR string here is the information that is swept to the surface, usually the link address. //4, stop scanning animation, display data.
According to the scanned link address, jump to the corresponding interface, display the data } }
iOS development----Scan QR code, barcode