QR code scanning and iOS scanning in ios

Source: Internet
Author: User

QR code scanning and iOS scanning in ios

With the extensive promotion, more and more people will use the QR code in their daily lives.

Therefore, many apps gradually add the QR code element. Today, I will introduce you to the iOS7 system, which can be manually designed for QR code scanning.

QRCodeViewController is a class for creating QR code scanning. Today, I wrote this similar to the implementation in the official documentation. You can directly scan the QR code to jump to the link corresponding to the current QR code. If it is an application, the app details in the appStore are displayed.

 

In addition to this, many third-party libraries, such as ZBarSDK, are currently used to achieve better results than self-written ones. If you have a chance later, I will introduce ZBarSDK usage and frequently encountered problems and solutions.

The main implementation is as follows:

 

# Import "QRCodeViewController. h"

# Import <AVFoundation/AVFoundation. h>

 

@ Interface QRCodeViewController () <AVCaptureMetadataOutputObjectsDelegate>

{

BOOL _ isReading;

}

 

@ Property (weak, nonatomic) IBOutlet UIView * ShowView;

 

 

@ Property (nonatomic, strong) UIView * boxView;

// @ Property (nonatomic, assign) BOOL * isReading;

@ Property (nonatomic, strong) CALayer * scanLayer;

 

 

@ Property (nonatomic, strong) UILabel * urlLabel;

 

-(BOOL) startReading;

-(Void) stopReading;

 

// Capture sessions

@ Property (nonatomic, strong) AVCaptureSession * captureSession;

 

// Display layer

@ Property (nonatomic, strong) AVCaptureVideoPreviewLayer * videoPreviewLayer;

 

@ End

 

@ Implementation QRCodeViewController

 

 

-(Void) viewDidLoad {

[Super viewDidLoad];

UIButton * btn = [UIButton buttonWithType: UIButtonTypeSystem];

Btn. frame = CGRectMake (0, 30, 50, 30 );

[Btn setImage: [UIImage imageNamed: @ "return @ 2x"] forState: UIControlStateNormal];

[Btn addTarget: self action: @ selector (leftClicked) forControlEvents: UIControlEventTouchUpInside];

[Self. view addSubview: btn];

_ CaptureSession = nil;

// _ IsReading = NO;

[Self startReading];

}

-(Void) leftClicked {

[Self dismissViewControllerAnimated: YES completion: nil];

}

 

 

-(BOOL) startReading {

NSError * error = nil;

// 1. initialize the capture device (AVCaptureDevice) in the AVMediaTypeVideo type.

AVCaptureDevice * captureDevice = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];

// 2. Use captureDevice to create an input stream

AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice: captureDevice error: & error];

If (! Input ){

NSLog (@ "% @", [error localizedDescription]);

Return NO;

}

// 3. Create a media data output stream

AVCaptureMetadataOutput * captureMedataOutput = [[AVCaptureMetadataOutput alloc] init];

// 4. instantiate the capture session

_ CaptureSession = [[AVCaptureSession alloc] init];

// 4.1 Add the input stream to the session

[_ CaptureSession addInput: input];

// 4.2 Add the media output stream to the session

[_ CaptureSession addOutput: captureMedataOutput];

// 5. Create a serial queue and add the media output stream to the queue

Dispatch_queue_t dispatchQueue;

DispatchQueue = dispatch_queue_create ("myQueue", NULL );

// 5.1 set proxy

[CaptureMedataOutput setMetadataObjectsDelegate: self queue: dispatchQueue];

// 5.2 set the output media data type to QRCode

[CaptureMedataOutput setMetadataObjectTypes: [NSArray arrayWithObject: AVMetadataObjectTypeQRCode];

// 6. instantiate the preview Layer

_ VideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession: _ captureSession];

// 7. Set the preview layer filling mode

[_ VideoPreviewLayer setVideoGravity: AVLayerVideoGravityResizeAspectFill];

// 8. Set the frame of the layer

[_ VideoPreviewLayer setFrame: _ ShowView. layer. bounds];

// 9. Add the layer to the preview view layer.

[_ ShowView. layer addSublayer: _ videoPreviewLayer];

// 10. Set the scan range

CaptureMedataOutput. rectOfInterest = CGRectMake (0.2f, 0.2f, 0.8f, 0.8f );

// 10.1. Set the scan box

_ BoxView = [[UIView alloc] initWithFrame: CGRectMake (_ ShowView. bounds. size. width * 0, _ ShowView. bounds. size. height * 0, _ ShowView. bounds. size. width, _ ShowView. bounds. size. height)];

_ BoxView. layer. borderColor = [UIColor greenColor]. CGColor;

_ BoxView. layer. borderWidth = 1.0f;

[_ ShowView addSubview: _ boxView];

// 10.2 scanned line

_ ScanLayer = [[CALayer alloc] init];

_ ScanLayer. frame = CGRectMake (0, 0, _ boxView. bounds. size. width, 2 );

_ ScanLayer. backgroundColor = [UIColor cyanColor]. CGColor;

[_ BoxView. layer addSublayer: _ scanLayer];

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval: 0.2f target: self selector: @ selector (moveScanLayer :) userInfo: nil repeats: YES];

[Timer fire];

// 11. Start scanning

[_ CaptureSession startRunning];

Return 1;

}

 

-(Void) moveScanLayer :( NSTimer *) timer {

CGRect frame = _ scanLayer. frame;

If (_ boxView. frame. size. height <= _ scanLayer. frame. origin. y ){

Frame. origin. y = 0;

_ ScanLayer. frame = frame;

}

Else {

Frame. origin. y + = 5;

[UIView animateWithDuration: 0.1 animations: ^ {

_ ScanLayer. frame = frame;

}];

}

}

 

 

-(Void) stopReading {

// [_ CaptureSession stopRunning];

// _ CaptureSession = nil;

// [_ ScanLayer removeFromSuperlayer];

// [_ VideoPreviewLayer removeFromSuperlayer];

}

 

 

# Pragma mark-AVCaptureMetadataOutputObjectsDelegate

-(Void) captureOutput :( AVCaptureOutput *) captureOutput didOutputMetadataObjects :( NSArray *) metadataObjects fromConnection :( AVCaptureConnection *) connection {

// Determine whether data exists

If (metadataObjects! = Nil & [metadataObjects count]> 0 ){

AVMetadataMachineReadableCodeObject * metadataObj = [metadataObjects objectAtIndex: 0];

// Determine the type of the returned data

If ([[metadataObj type] isEqualToString: AVMetadataObjectTypeQRCode]) {

[Self defined mselecw.mainthread: @ selector (setUrl :) withObject: [metadataObj stringValue] waitUntilDone: NO];

// [[UIApplication sharedApplication] openURL: url];

[Self defined mselecw.mainthread: @ selector (stopReading) withObject: nil waitUntilDone: NO];

_ IsReading = NO;

}

}

}

 

-(Void) setUrl :( NSString *) text {

NSURL * url = [NSURL URLWithString: text];

[[UIApplication sharedApplication] openURL: url];

}

 

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.