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
UI Effects
Iboutlet, ibaction as follows:
@property (Weak, nonatomic) iboutlet UIView **startbtn; -(Ibaction) startstopreading: (ID) sender;
And then it's all about the code thing.
Second, the controller ViewController.h
First import the Avfoundation framework
#import <AVFoundation/AVFoundation.h>
Then the controller implements the Avcapturemetadataoutputobjectsdelegate protocol
@interface Viewcontroller () <AVCaptureMetadataOutputObjectsDelegate>
The overall property is as follows:
@property (Strong, Nonatomic) UIView **scanlayer; -(BOOL) startreading; -(void) stopreading; // Capture Session @property (nonatomic, Strong) avcapturesession *capturesession; // Show Layer@property (nonatomic, strong) Avcapturevideopreviewlayer *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]); returnNO; } //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:self queue: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.2ftarget:self selector: @selector (movescanlayer:) Userinfo:nil Repeats:yes]; [Timer fire];
//10. Start Scanning[_capturesession startrunning]; returnYES;}
Implementing the Avcapturemetadataoutputobjectsdelegate protocol approach
#pragmamark-avcapturemetadataoutputobjectsdelegate-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects fromConnection :(Avcaptureconnection *) connection{//determine if there is data if(metadataobjects! = Nil && [metadataobjects count] >0) {Avmetadatamachinereadablecodeobject*metadataobj = [Metadataobjects objectatindex:0]; //determine the data type of the postback if([[[Metadataobj Type] isequaltostring:avmetadataobjecttypeqrcode]) {[_lblstatus Performselectoronmainthrea D: @selector (setText:) withobject:[metadataobj stringvalue] waituntildone:no]; [Self Performselectoronmainthread: @selector (stopreading) Withobject:nil Waituntildone:no]; _isreading=NO; } }}
Implement Timer Method Movescanlayer: (Nstimer *) timer
-(void) Movescanlayer: (Nstimer *) timer{ = _scanlayer.frame; if (_boxview.frame.size.height < _scanlayer.frame.origin.y) { 0; = frame; } Else { 5; [UIView animatewithduration: 0.1 animations:^{ = frame; }];} }
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];}
Done
Sample Source Address: Https://github.com/WuKongCoo1/QRCodeReader
IOS: native QR code scan