After Apple upgraded to IOS7, its PassBook comes with the QR code scanning function. Therefore, the QR code function does not need to be used in third-party libraries.
Before use, please first import AVFoundation. frameWork
// YHQViewController. m
// ReadQRCode
//
// Created by apple on 13-12-22.
// Copyright (c) 2013 apple. All rights reserved.
//
# Import "YHQViewController. h"
# Import <AVFoundation/AVFoundation. h>
@ InterfaceYHQViewController () <AVCaptureMetadataOutputObjectsDelegate>
// IBOutletUILabel * captureLabe is a line in the storyBoard you have created to display the information of the obtained QR code.
@ End
@ Implementation YHQViewController
-(Void) viewDidLoad
{
[SuperviewDidLoad];
# Pragma mark-read the QR code
-(Void) readQRcode
{
// 1. Camera Device
AVCaptureDevice * device = [avcapturedevicedefadevicdevicewithmediatype: AVMediaTypeVideo];
// 2. Set Input
// Because the simulator does not have a camera, it is best to make a judgment here
NSError * error = nil;
AVCaptureDeviceInput * input = [AVCaptureDeviceInputdeviceInputWithDevice: device error: & error];
NSLog (@ "no camera-% @", error. localizedDescription );
}
// 3. Set the output (Metadata)
AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutputalloc] init];
// 3.1 set the output proxy
// Note: the main thread queue is used for synchronization. Other queues are used for synchronization, which may lead to poor user experience.
[Output setMetadataObjectsDelegate: selfqueue: dispatch_get_main_queue ()];
// [Output setMetadataObjectsDelegate: self queue: dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
// 4. Shooting sessions
AVCaptureSession * session = [[AVCaptureSessionalloc] init];
// Add session Input and Output
[Session addInput: input];
[Session addOutput: output];
// 4.1 set the output format
// Prompt: You must set the output of the session to output first, and then specify the output metadata type!
[Output setMetadataObjectTypes: @ [AVMetadataObjectTypeQRCode];
// 5. Set the preview layer (used to enable users to see the scan status)
AVCaptureVideoPreviewLayer * preview = [AVCaptureVideoPreviewLayerlayerWithSession: session];
// 5.1 set properties of the preview Layer
[Preview setVideoGravity: AVLayerVideoGravityResizeAspectFill];
// 5.2 set the preview layer size
[Preview setFrame: self. view. bounds];
// 5.3 add a layer to the view
[Self. view. layerinsertSublayer: preview atIndex: 0];
Self. previewLayer = preview;
// 6. Start the session
[Session startRunning];
Self. session = session;
}
# Pragma mark-output proxy method
// This method is used to identify the QRCode and complete the conversion.
// If the content of QRCode is greater, the conversion takes longer.
-(Void) captureOutput :( AVCaptureOutput *) captureOutput didOutputMetadataObjects :( NSArray *) metadataObjects fromConnection :( AVCaptureConnection *) connection
{
// Scan frequently and call the proxy method
// 1. If the scan is complete, stop the session
[Self. sessionstopRunning];
// 2. Delete the preview Layer
[Self. previewLayerremoveFromSuperlayer];
NSLog (@ "% @", metadataObjects );
// 3. Set the scan result to be displayed on the page
If (metadataObjects. count> 0 ){
AVMetadataMachineReadableCodeObject * obj = metadataObjects [0];
// Prompt: If you need to scan url or business card information, you can expand it here!
_ CaptureLabel. text = obj. stringValue;
}
}
// Click the link event of the button added in the storyBoard. When you click the button, the system prompts the user to open the camera and scan.
-(IBAction) capture {
// Scan the QR code
[SelfreadQRcode];
}
@ End