Avfoundation supports the following one-and two-dimensional code scanning:
- QR Code
- Aztec
- EAN13
- EAN8
- Upc-e
- PDF417
- Code 93
- Code 39
- Code 41 Mode
QR code is the two-dimensional code we are familiar with.
Open VIEWCONTROLLER.M and add the following instance variable:
Avcapturemetadataoutput *_metadataoutput;
When metadata is detected from a video frame, Avcapturemetadataoutput invokes the application's callback function. AV Foundation supports two types of metadata: machine-readable encoding and face recognition.
Add the following code to the end of setupcapturesession to implement the capture and processing of the metadata:
_metadataoutput = [[Avcapturemetadataoutput alloc] init]; dispatch_queue_t metadataqueue = dispatch_queue_create ("Com.yaming.ColloQR.metadata", 0); [_metadataoutput setmetadataobjectsdelegate:self queue:metadataqueue]; if ([_capturesession canaddoutput:_metadataoutput]) { [_capturesession addoutput:_metadataoutput]; }
Here, we first create an instance of Avcapturemetadataoutput and then create a dispatch queue for it, which prevents the main thread from blocking when the callback function is called.
Next, declare this class to follow the Avcapturemetadataoutputobjectsdelegate agreement:
@interface Viewcontroller () <AVCaptureMetadataOutputObjectsDelegate>
To implement the Proxy method:
-(void) Captureoutput: (Avcaptureoutput *) captureoutput didoutputmetadataobjects: (Nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) connection{ [Metadataobjects enumerateobjectsusingblock:^ ( Avmetadataobject *obj, nsuinteger idx, BOOL *stop) { NSLog (@ "Metadata:%@", obj); }];}
Whenever the Avcapturemetadataoutput class detects new metadata, the Captureoutput method is called, and in the Captureoutput method, we print all the detected metadata.
To modify the Startrunning method, add the following code at the end:
_metadataoutput.metadataobjecttypes = _metadataoutput.availablemetadataobjecttypes;
The purpose of this is to set the metadata type to be detected for all types.
Compile and run, scan the following two-D code to get the following output:
2015-03-21 14:51:42.289 colloqr[680:176124] Metadata: <avmetadatamachinereadablecodeobject:0x170221540, type= " Org.iso.QRCode ", bounds={0.4,0.2 0.1x0.3}>corners {0.4,0.5 0.5,0.5 0.5,0.2 0.4,0.2}, Time 31026866447458, StringVal UE "ios QR code scan"
This completes the function of QR code scanning using AV Foundation. As you can see from log, there are bounds and conrners information, and in the next section we will use this information to provide visual feedback to the user when the code is successful.
Reprint Please specify source: http://blog.csdn.net/yamingwu/article/details/44515625
IOS AV Foundation qr code scan 02 Sweep code