Learn the QR code of iOS, and quickly open the camera to read the QR code.
The procedure is as follows:
To read the QR code, you must import it to the AVFoundation framework.#import
1: use the camera to identify the content in the QR code (not the simulator ).
2: input (CAMERA ).
3: The session converts the two-dimensional code image collected by the camera into string data.
4: output (data ).
5: The scan scenario is displayed by the preview layer.
#import ViewController.h
#import
@interface ViewController ()vcapturemetadataoutputobjectsdelegate>
@property (nonatomic, strong) AVCaptureSession *session;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//1. Instantiate shooting equipment
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//2. Set input device
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
//3. Set metadata output
//3.1 instantiate capture metadata output
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
//3.3 setting the output data agent
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//4. Add shooting session
//4.1 instantiate shooting session
AVCaptureSession *session = [[AVCaptureSession alloc] init];
//4.2 add session input
[session addInput:input];
//4.3 add session output
[session addOutput:output];
//4.3 to set the output data type, you need to add the metadata output to the session before specifying the metadata type, otherwise an error will be reported
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
self.session = session;
//5. Video preview layer
//5.1 instantiate the preview layer and transfer the session to tell the layer what to display in the future
AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession:_session];
preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
preview.frame = self.view.bounds;
//5.2 inserting layers into the current view
[self.view.layer insertSublayer:preview atIndex:100];
self.previewLayer = preview;
//6. Start session
[_session startRunning];
}
#Pragma mark - avcapturemetadataoutputobjectsdelegate proxy method
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
//Frequently scan and call proxy methods
//1. If the scan is complete, stop the session
[self.session stopRunning];
//2. Delete preview layer
[self.previewLayer removeFromSuperlayer];
NSLog(@%@, metadataObjects);
//3. The setting interface displays the scanning results
if (metadataObjects.count > 0) {
AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
//Tip: if you need to scan URL or business card information, you can extend it here!
// _label.text = obj.stringValue;
NSLog(@%@, obj.stringValue);
}
}