#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