iOS system method scan QR code

Source: Internet
Author: User






#import "RootViewController.h"
#import avfoundation/avfoundation.h//The angle brackets on both sides are not showing.

@interface Rootviewcontroller () avcapturemetadataoutputobjectsdelegate//Agreement ibid.

@property (nonatomic, strong) UILabel *showlabel;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) avcapturesession *session;
@property (nonatomic, strong) Avcapturevideopreviewlayer *preview;
@end

@implementation Rootviewcontroller

-(void) Viewdidload {
[Super Viewdidload];

Self.showlabel = [[UILabel alloc] Initwithframe:cgrectmake (self.view.bounds.size.width-40, 30)];
Self.showLabel.backgroundColor = [Uicolor Yellowcolor];
[Self.view AddSubview:self.showLabel];

Self.button = [UIButton Buttonwithtype:uibuttontypesystem];
Self.button.frame = CGRectMake (self.view.bounds.size.width-40, 300);
[Self.button settitle:@ "Start" forstate:uicontrolstatenormal];
[Self.button settitle:@ "Stop" forstate:uicontrolstateselected];
[Self.button addtarget:self Action: @selector (ButtonClick:) forcontrolevents:uicontroleventtouchupinside];
[Self.view AddSubview:self.button];
}

-(void) ButtonClick: (UIButton *) button {
button.selected =!button.selected;
if (button.selected) {
[Self startreadingmachinereadablecodeobjects:@[avmetadataobjecttypeqrcode] inView:self.view];
} else {
[Self stopreading];
}
}

Start scanning
-(void) Startreadingmachinereadablecodeobjects: (Nsarray *) codeobjects InView: (UIView *) Preview {
Camera devices
Avcapturedevice *capturedevice = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];
Nserror *error = nil;

Setting the input port
Avcapturedeviceinput *input = [Avcapturedeviceinput deviceinputwithdevice:capturedevice error:&error];
if (Error | |!input) {
NSLog (@ "error:%@", [error description]);
Return
}

Sessions session, add input to session
Self.session = [[Avcapturesession alloc] init];
[Self.session Addinput:input]; Add input to session

Set export, join session, set output parameters
Avcapturemetadataoutput *output = [[Avcapturemetadataoutput alloc] init];
[Self.session Addoutput:output];

[Output SETMETADATAOBJECTTYPES:CODEOBJECTS];
[Output Setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()]; Using the main thread queue, the corresponding synchronization, the use of other queues, corresponding to different steps, it is easy for users to produce a bad experience

Set up the presentation layer (preview layer)
Self.preview = [Avcapturevideopreviewlayer layerWithSession:self.session]; Set Preview Layer Information
Self.preview.frame = Preview.bounds;
[Preview.layer InsertSublayer:self.preview atindex:0]; Add to view

[Self beginreading];
}

Start session
-(void) beginreading
{
[Self.session startrunning];
}

Close session
-(void) stopreading
{
self.button.selected = NO;
[Self.session stoprunning];
[Self.preview Removefromsuperlayer];
}

Identify the QR code and parse it into a string
#pragma mark-
-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection
{
[Self stopreading];

Avmetadataobject *metadata = [Metadataobjects objectatindex:0];
NSString *codestr = nil;
if ([Metadata respondstoselector: @selector (stringvalue)]) {
CODESTR = [(Avmetadatamachinereadablecodeobject *) metadata stringvalue];
}

Self.showLabel.text = Codestr;
}

@end

iOS system method scan QR code

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.