Do iOS QR code scan, there are two third-party libraries to choose from, Zbar and zxing. Today is to introduce the iOS7.0 after the Avfoundation framework provides a native QR code scan.
First, you need to add the Avfoundation.framework framework to the "Link Binary with Libraries" in your project build phase, and then you can start.
First, to prepare the work, build the UI
Iboutlet, ibaction as follows:
*viewpreview; *lblstatus; *startbtn;-(ibaction) startstopreading: (ID) sender;
Second, the controller ViewController.h
First import the Avfoundation framework
<avfoundation/avfoundation.h>
Then the controller implements the Avcapturemetadataoutputobjectsdelegate protocol
@interface Viewcontroller ()<avcapturemetadataoutputobjectsdelegate>
The overall property is as follows:
*boxview; @property (nonatomic) BOOL isreading; *scanlayer;-(BOOL) startreading;-(void) stopreading; //capture session *capturesession; Display layer*videopreviewlayer;
Then class in the Viewdidload method.
-(void) viewdidload { [super viewdidload]; Nil; NO; }
The next step is to implement the Startreading method (which is the point)
-(BOOL) startreading {Nserror *error; //1. Initialize the capture device (avcapturedevice), type AvmediatypevideoAvcapturedevice *capturedevice = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo]; //2. Creating an input stream with CapturedeviceAvcapturedeviceinput *input = [Avcapturedeviceinput deviceinputwithdevice:capturedevice error:&error]; if (!input) { NSLog (@"%@ ", [Error localizeddescription]); return NO;} //3. Create a media data output streamAvcapturemetadataoutput *capturemetadataoutput = [[Avcapturemetadataoutput alloc] init]; //4. Instantiating a capture session_capturesession = [[Avcapturesession alloc] init]; //4.1. Add an input stream to the session[_capturesession Addinput:input]; //4.2. Add media output stream to session[_capturesession Addoutput:capturemetadataoutput]; //5. Create a serial queue and add the media output stream to the queuedispatch_queue_t Dispatchqueue;Dispatchqueue = Dispatch_queue_create ("Myqueue ", NULL); //5.1. Setting up the agent[Capturemetadataoutput setmetadataobjectsdelegate:selfQueue:dispatchqueue]; //5.2. Set the output media data type to QRCode[Capturemetadataoutput Setmetadataobjecttypes:[nsarray Arraywithobject:avmetadataobjecttypeqrcode]; //6. Instantiating a preview layer_videopreviewlayer = [[Avcapturevideopreviewlayer alloc] initwithsession:_capturesession]; //7. Set how the preview layer fills[_videopreviewlayer Setvideogravity:avlayervideogravityresizeaspectfill]; //8. Set the frame of the layer[_videopreviewlayer Setframe:_viewpreview.layer.bounds]; //9. Add a layer to a layer on the preview view[_viewpreview.layer Addsublayer:_videopreviewlayer]; //10. Setting the Scan RangeCapturemetadataoutput.rectofinterest = CGRectMake (0.2f,0.2f,0.8f,0.8f); //10.1. Scan Box_boxview = [[UIView alloc] Initwithframe:cgrectmake (_viewpreview.bounds.size.width *0.2f, _viewpreview.bounds.size.height *0.2f, _viewpreview.bounds.size.width-_viewpreview.bounds.size.width *0.4f, _viewpreview.bounds.size.height-_viewpreview.bounds.size.height *0.4f)];_boxview.layer.bordercolor = [Uicolor Greencolor]. Cgcolor;_boxview.layer.borderwidth =1.0f;[_viewpreview Addsubview:_boxview]; //10.2. Scan line _scanlayer = [[ Calayer alloc] init]; _scanlayer.frame = CGRectMake (0, 0, _ BoxView.bounds.size.width, 1); _scanlayer.backgroundcolor = [Uicolor Browncolor]. Cgcolor; [_boxview.layer Addsublayer:_scanlayer]; Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:0.2f target:self Selector: @selector (movescanlayer:) Userinfo:nil Repeats:yes]; [timer fire]; //10. Start scanning [_ Capturesession startrunning]; return YES;
Implementing the Avcapturemetadataoutputobjectsdelegate protocol approach
#pragmamark-avcapturemetadataoutputobjectsdelegate-(void)Captureoutput: (Avcaptureoutput *) CaptureoutputDidoutputmetadataobjects: (Nsarray *) metadataobjectsFromconnection: (Avcaptureconnection *) connection{ Determine if there is data if (metadataobjects! =Nil && [metadataobjects count] >0) { Avmetadatamachinereadablecodeobject *metadataobj = [metadataobjectsObjectatindex:0]; Determine the data type of the postback if ([[Metadataobj type]Isequaltostring:Avmetadataobjecttypeqrcode]) { [_lblstatus Performselectoronmainthread: @selector (settext:) withobject:[metadataobj StringValue] waituntildone: no]; [self Performselectoronmainthread: @selector (stopreading) withobject:nil waituntildone:no]; _isreading = no; }}} /span>
Implement Timer Method Movescanlayer: (Nstimer *) timer
-(void) Movescanlayer: (Nstimer *) timer{CGRect frame = _scanlayer.frame;if (_boxview.frame.size.height<_SCANLAYER.FRAME.ORIGIN.Y) { FRAME.ORIGIN.Y = 0;< Span class= "indent" > _scanlayer.frame = frame ; }else{ FRAME.ORIGIN.Y + = 5; [uiview animatewithduration:0.1 animations:^{ _scanlayer.frame = frame; }]; } /span>
Implementing Start and Stop methods
-(Ibaction) startstopreading: (ID) Sender { if (!_isreading) { if ([self startreading]) { [_startbtn Settitle:@"Stop "Forstate:uicontrolstatenormal"; [_lblstatus SetText:@"Scanning for QR Code "]; } } else{ [self stopreading]; [_startbtn settitle:@ "start! "Forstate:uicontrolstatenormal"; } _isreading =!_isreading;} -(void) stopreading{ [_capturesession stoprunning]; _capturesession = nil; [_scanlayer Removefromsuperlayer]; [_videopreviewlayer removefromsuperlayer];}
Native QR code scan of iOS