Home>

This article introduces you to the iOS native QR code scanning

Do QR code scanning for ios,There are two third-party libraries to choose from,zbar and zxing. Today I will introduce the native QR code scanning provided by the avfoundation framework after ios7.0.

First you need to add the avfoundation.framework framework to the "link binary with libraries" in the build phase of your project, and then you can start.

First, make preparations,Build ui

ui effect as shown

The iboutlet and ibaction are as follows:

@property (weak, nonatomic) iboutlet uiview * viewpreview;
@property (weak, nonatomic) iboutlet uilabel * lblstatus;
@property (weak, nonatomic) iboutlet uibutton * startbtn;
-(ibaction) startstopreading:(id) sender;

The next thing is code

Controller viewcontroller.h

First import the avfoundation framework

#import<avfoundation/avfoundation.h>
</pre>
</div>
<p>
Then the controller implements the avcapturemetadataoutputobjectsdelegate protocol
</p>
<div>
<pre>
@interface viewcontroller ()<avcapturemetadataoutputobjectsdelegate>
</pre>
</div>
<p>
<strong>
The overall property is as follows:
</strong>
</p>
<div>
<pre>
@property (strong, nonatomic) uiview * boxview;
@property (nonatomic) bool isreading;
@property (strong, nonatomic) calayer * scanlayer;
-(bool) startreading;
-(void) stopreading;

//capture session

@property (nonatomic, strong) avcapturesession * capturesession;

//Show layer

@property (nonatomic, strong) avcapturevideopreviewlayer * videopreviewlayer;
</pre>
</div>
<p>
Then initialized in viewdidload method
</p>
<div>
<pre>
-(void) viewdidload {
  [super viewdidload];
  _capturesession=nil;
   _isreading=no;
}

Next implement the startreading method (this is the point)

-(bool) startreading {
 nserror * error;
 //1. Initialize the capture device (avcapturedevice), the type is avmediatypevideo
 avcapturedevice * capturedevice=[avcapturedevice defaultdevicewithmediatype:avmediatypevideo];
 //2. Create input stream with capturedevice
 avcapturedeviceinput * input=[avcapturedeviceinput deviceinputwithdevice:capturedevice error:&error];
 if (! input) {
  nslog (@ "%@", [error localizeddescription]);
  return no;
 }
 //3. Create media data output stream
 avcapturemetadataoutput * capturemetadataoutput=[[avcapturemetadataoutput alloc] init];
 //4. Instantiate the capture session
 _capturesession=[[avcapturesession alloc] init];
 //4.1. Add the input stream to the session
 [_capturesession addinput:input];
 //4.2. Add media output stream to the session
 [_capturesession addoutput:capturemetadataoutput];
 //5. Create a serial queue,And add the media output stream to the queue
 dispatch_queue_t dispatchqueue;
 dispatchqueue=dispatch_queue_create ("myqueue", null);
 //5.1. Set proxy
 [capturemetadataoutput setmetadataobjectsdelegate:self queue:dispatchqueue];
 //5.2. Set the output media data type to qrcode
 [capturemetadataoutput setmetadataobjecttypes:[nsarray arraywithobject:avmetadataobjecttypeqrcode]];
 //6. Instantiate the preview layer
 _videopreviewlayer=[[avcapturevideopreviewlayer alloc] initwithsession:_capturesession];
 //7. Set the preview layer fill method
 [_videopreviewlayer setvideogravity:avlayervideogravityresizeaspectfill];
 //8. Set the frame of the layer
 [_videopreviewlayer setframe:_viewpreview.layer.bounds];
 //9. Add the layer to the layer of the preview view
 [_viewpreview.layer addsublayer:_videopreviewlayer];
 //10. Set scan range
 capturemetadataoutput.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.4 f, _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;
}

Implement avcapturemetadataoutputobjectsdelegate protocol method

#pragma mark-avcapturemetadataoutputobjectsdelegate
-(void) captureoutput:(avcaptureoutput *) captureoutput didoutputmetadataobjects:(nsarray *) metadataobjects fromconnection:(avcaptureconnection *) connection
{
 //Judge whether there is data
 if (metadataobjects!=nil&&[metadataobjects count]>0) {
  avmetadatamachinereadablecodeobject * metadataobj=[metadataobjects objectatindex:0];
  //Judge the data type returned
  if ([[metadataobj type] isequaltostring:avmetadataobjecttypeqrcode]) {
   [_lblstatus performselectoronmainthread:@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
{
 cgrect frame=_scanlayer.frame;
 if (_boxview.frame.size.height<_scanlayer.frame.origin.y) {
  frame.origin.y=0;
  _scanlayer.frame=frame;
 } else {
  frame.origin.y +=5;
  [uiview animatewithduration:0.1 animations:^ {
   _scanlayer.frame=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];
}

The above content is all the content of this article to introduce you to the iOS two-dimensional code scanning.Hope you like it.

ios
  • Previous NodeJS and HTML5 combined implementation method of dragging multiple files to upload to the server
  • Next Method for traversing object properties and values ​​in js