Scan QR code needs to use avfoundation frame, need to include #import <AVFoundation/AVFoundation.h>
1. Create a capture session
Avcapturesession *session = [[Avcapturesession alloc] init];
2. Add input device (data from camera input)
Avcapturedevice *device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];
Avcapturedeviceinput *input = [Avcapturedeviceinput deviceinputwithdevice:device error:nil];
[Session Addinput:input];
3. Add Output data (example Object----class object-------the Meta-class object)
Avcapturemetadataoutput *output = [[Avcapturemetadataoutput alloc] init];
[Output Setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()]; The definition is executed in the main thread, or in a child thread,
Requires Controller compliance Protocol:<avcapturemetadataoutputobjectsdelegate>
[Session Addoutput:output];
3.1. Set the type of the input metadata (type is the QR code data)
[Output SETMETADATAOBJECTTYPES:@[AVMETADATAOBJECTTYPEQRCODE]];
4. Add a scan layer
Avcapturevideopreviewlayer *layer = [Avcapturevideopreviewlayer layerwithsession:session];
Layer.frame = Self.view.bounds; Full Screen display scan box
[Self.view.layer Addsublayer:layer];
5. Start scanning
[Session startrunning];
This method is executed when the data is scanned
-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection
{
if (Metadataobjects.count > 0) {
Avmetadatamachinereadablecodeobject *object = [Metadataobjects lastobject];
NSLog (@ "%@", Object.stringvalue);
Stop scanning
[Self.session stoprunning];
To remove a preview layer
[Self.layer Removefromsuperlayer];
} else {
NSLog (@ "no data scanned");
}
}
How iOS scans QR codes