Ios qr code scanning and ios Scanning
This blog is a supplement to the previous blog, with less content.
Because the QR code scan needs to be tested on a real machine, it is too bad to do it.
Below is the code (a clear comment has been written in the Code)
/// ViewController. m // cx qr code scan /// Created by ma c on 16/4/12. // Copyright©2016 bjsxt. all rights reserved. // # import "ViewController. h "# import <AVFoundation/AVFoundation. h> @ interface ViewController () <AVCaptureMetadataOutputObjectsDelegate> @ end @ implementation ViewController-(void) viewDidLoad {}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// create capture session AVCaptureSession * session = [[AVCaptureSession alloc] init]; // Add the input device AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType: @ "AVMediaTypeVideo"]; AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice: device error: nil]; [session addInput: input]; // Add the output data AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc] init]; [output setMetadataObjectsDelegate: self queue: dispatch_get_main_queue ()]; [session addOutput: output]; // tell the metadata type to be a QR code type. // note that this method will crash after adding. // The real machine is required for testing, but it will not be able to [output setMetadataObjectTypes: @ [AVMetadataObjectTypeQRCode]; // Add scan layer AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession: session]; layer. frame = CGRectMake (10, 20, self. view. frame. size. width (400); [self. view. layer addSublayer: layer]; // start to scan [session startRunning]; // when appropriate, perform the following operations: // stop scanning // [session stopRunning]; // remove layer // [layer removeFromSuperlayer];}-(void) captureOutput :( AVCaptureOutput *) captureOutput didOutputMetadataObjects :( NSArray *) metadataObjects fromConnection :( AVCaptureConnection *) connection {// metadataObjects is the scanned data AVMetadataMachineReadableCodeObject * objc = [metadataObjects lastObject]; // The NSLog (@ "% @", objc. stringValue) ;}@ end