iOS QR Code scan (System comes with QR code scan)

Source: Internet
Author: User

iOS QR code scan

Here to introduce how to use the system comes with the QR code scanning method and some simple animation!

Operation Steps:

1). First you need to build a UI interface. I used two ImageView and a label.

          

2), you need to import in your current controller

#import <AVFoundation/AVFoundation.h>

<AVCaptureMetadataOutputObjectsDelegate> Agent

3), defined in @interface    

@property (nonatomic,strong) avcapturedevice * device;

@property (nonatomic,strong) avcapturedeviceinput * input;

@property (nonatomic,strong) avcapturemetadataoutput * OUTPUT;

@property (Nonatomic,strong) avcapturesession * session;

@property (nonatomic,strong) avcapturevideopreviewlayer * preview;

4), drag the two picture attributes in the UI interface

@property (Strong, nonatomic) Iboutlet Uiimageview *prereferimage;//This is the frame picture in the UI interface

@property (Nonatomic,strong) iboutlet Uiimageview * imageline;//This is a green line picture in the UI interface

5), is to put the following code into your controller

-(void) viewdidload

{

[Super Viewdidload];

[Self setupcamera];//set camera

}

Setting up the camera

-(void) Setupcamera

{

Device Properties

Self.device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];

Input Property

Nserror *error = nil;

Self.input = [Avcapturedeviceinput deviceInputWithDevice:self.device error:&error];

if (Error) {

NSLog (@ "error");

Return

}

Output Property

Self.output = [[Avcapturemetadataoutput alloc]init];

[Self.output setmetadataobjectsdelegate:self Queue:dispatch_get_main_queue ()];

Session Properties

Self.session = [[Avcapturesession alloc]init];

[Self.session Setsessionpreset:avcapturesessionpresethigh];

if ([Self.session canAddInput:self.input])

{

[Self.session AddInput:self.input];

}

if ([Self.session canAddOutput:self.output])

{

[Self.session AddOutput:self.output];

}

self.output.metadataObjectTypes [Email protected][avmetadataobjecttypeqrcode];//here is the type of Set scan

Preview Property

Self.preview = [Avcapturevideopreviewlayer layerWithSession:self.session];

self.preview.videoGravity = Avlayervideogravityresizeaspectfill;

Self.preview.frame = Self.view.bounds;

[Self.view.layer InsertSublayer:self.preview Below:self.preReferImage.layer];

Start Property

[Self.session startrunning];

[Self setupanimation];//This is the top and bottom scan animation of the horizontal line, which can be added without adding

}

/** Note: If you need to add this line scan animation need to add the following properties in @interface * *

{

int linevalue; Save the frame value of the horizontal line

Nstimer * timer;//Timer Let the horizontal line move around

BOOL down;//Down

BOOL up;//up

}

Horizontal line Animation

-(void) setupanimation

{

CGFloat beginlinevalue = Cgrectgetminy (self.preReferImage.frame);

down = YES;

up = NO;

Linevalue =beginlinevalue;

[Timer invalidate];

Timer = [Nstimer scheduledtimerwithtimeinterval:.01

Target:self selector: @selector (animation1) Userinfo:nil Repeats:yes];

}

-(void) Animation1

{

if (down) {

CGFloat maxValue = Cgrectgetmaxy (self.preReferImage.frame);

linevalue++;

Self.imageLine.frame = CGRectMake (Self.imageline.frame.origin.x,linevalue, Self.imageLine.frame.size.width, Self.imageLine.frame.size.height);

if (Linevalue ==maxvalue) {

up = YES;

}

}

if (UP) {

CGFloat minValue = Cgrectgetminy (self.preReferImage.frame);

linevalue-=2;

Self.imageLine.frame = CGRectMake (self.imageline.frame.origin.x, Linevalue, Self.imageLine.frame.size.width, Self.imageLine.frame.size.height);

if (Linevalue ==minvalue) {

down = YES;

up = NO;

}

}

}

/** This is the proxy method that must be implemented, and from this method you can get the scanned url*/

#pragma mark-avcapturemetadataoutputobjectsdelegate

-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection

{

NSString * STRINGVALUE;

if ([metadataobjects count] > 0) {

[Self.session stoprunning];

Avmetadatamachinereadablecodeobject * Metadataobj = [metadataobjects objectatindex:0];

StringValue = Metadataobj.stringvalue;

[Timer invalidate];//This is the stop animation after a successful scan

Timer = nil;

[Self requestparsing:stringvalue]; This is a successful scan after the network request method, you can comment

NSLog (@ "StringValue =%@", stringvalue);

}

}

/** Note:

1, the above example just shows how to use the system comes with the QR code scan, did not do a successful scan network access;

2, this QR code scan except the middle picture frame can see outside the other screen is also visible, my solution is to add a view with the current view is on the same level opaque on the

The frames used in this scan are:

Afnetworking This is for network requests.

Mbprogresshud This is used to load the network is the prompt, for example, when the network load, it will pop up a box: Loading ... Wait a minute!

And of course there are third-party QR code scans: Zxing and Zbar are available on GitHub.

*/

iOS QR Code scan (System comes with QR code scan)

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.