How iOS scans QR codes

Source: Internet
Author: User

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

Related Article

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.