Ios qr code scanning and ios Scanning

Source: Internet
Author: User

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

 

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.