Learn the QR code of iOS, and quickly open the camera to read the QR code.

Source: Internet
Author: User

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);
}
} 

 


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.