Native QR code scanning in iOS

Source: Internet
Author: User

Native QR code scanning in iOS

This article describes how to use the built-in system to scan QR code: click a button on the current page to create a scan VIEW. Careful friends can find that the title is changed and the return button is hidden. This code can be written by yourself, and it has little to do with this article... The Green Line will run up and down. Don't worry: D

The implementation result is as follows:

1. Add the framework AVFoundation. framework to the Project

2. Declare the Proxy: AVCaptureMetadataOutputObjectsDelegate. Define:

# Define SCANVIEW_EdgeTop 40.0 # define SCANVIEW_EdgeLeft 50.0 # define TINTCOLOR_ALPHA 0.2 // light transparency # define DARKCOLOR_ALPHA 0.5 // dark transparency # define VIEW_WIDTH [UIScreen mainScreen]. bounds. size. width # define VIEW_HEIGHT [UIScreen mainScreen]. bounds. size. height

 

3. declare the following attributes:

AVCaptureSession * session; // The UIView * AVCapView of the intermediate Bridge of input and output; // This view is used to place the scan box, cancel the button, and describe label UIView * _ QrCodeline; // move the Green Line NSTimer * _ timer up/down;

4. In a method (I click the scan button), create a scan interface and start scanning:

// Create a view to place the scan area, description label, and cancel the button UIView * tempView = [[UIView alloc] initWithFrame: CGRectMake (0, 0,320, [UIScreen mainScreen]. bounds. size. height)]; AVCapView = tempView; AVCapView. backgroundColor = [UIColor colorWithRed: 54.f/ 255 green: 53.f/ 255 blue: 58.f/ 255 alpha: 1]; UIButton * cancelBtn = [[UIButton alloc] initWithFrame: CGRectMake (15, [UIScreen mainScreen]. bounds. size. height-100, 50, 25)]; UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (15,268,290, 60)]; label. numberOfLines = 0; label. text = @ "Tip: Align the barcode or QR code to the center of the area above"; label. textColor = [UIColor grayColor]; [cancelBtn setTitle: @ "cancel" forState: canceled]; [cancelBtn setTitleColor: [UIColor grayColor] forState: UIControlStateNormal]; [cancelBtn addTarget: self action: @ selector (touchAVCancelBtn) forControlEvents: UIControlEventTouchUpInside]; [AVCapView addSubview: label]; [AVCapView addSubview: cancelBtn]; [self. view addSubview: AVCapView]; // draw the border UIView * topView = [[UIView alloc] initWithFrame: CGRectMake (SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, VIEW_WIDTH-2 * Rows, 1)]; topView. backgroundColor = [UIColor whiteColor]; [AVCapView addSubview: topView]; // draw the Left Border UIView * leftView = [[UIView alloc] initWithFrame: CGRectMake (callback, SCANVIEW_EdgeTop, 1, VIEW_WIDTH-2 * SCANVIEW_EdgeLeft)]; leftView. backgroundColor = [UIColor whiteColor]; [AVCapView addSubview: leftView]; // draw the UIView * rightView on the right border = [[UIView alloc] initWithFrame: CGRectMake (rows + VIEW_WIDTH-2 * Rows, SCANVIEW_EdgeTop, 1, VIEW_WIDTH-2 * SCANVIEW_EdgeLeft + 1)]; rightView. backgroundColor = [UIColor whiteColor]; [AVCapView addSubview: rightView]; // draw the border UIView * downView = [[UIView alloc] initWithFrame: CGRectMake (SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop + VIEW_WIDTH-2 * SCANVIEW_EdgeLeft, VIEW_WIDTH-2 * SCANVIEW_EdgeLeft, 1)]; downView. backgroundColor = [UIColor whiteColor]; [AVCapView addSubview: downView]; // draw the intermediate baseline _ QrCodeline = [[UIView alloc] initWithFrame: CGRectMake (callback + 1, SCANVIEW_EdgeTop, VIEW_WIDTH-2 * SCANVIEW_EdgeLeft-1, 2)]; _ QrCodeline. backgroundColor = [UIColor greenColor]; [AVCapView addSubview: _ QrCodeline]; // first let the baseline exercise to avoid time difference of the timer [UIView animateWithDuration: 1.2 animations: ^ {_ QrCodeline. frame = CGRectMake (rows + 1, VIEW_WIDTH-2 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop, VIEW_WIDTH-2 * SCANVIEW_EdgeLeft-1, 2) ;}]; [self timer mselector: @ selector (createTimer) withObject: nil afterDelay: 0.4]; AVCaptureDevice * device = [AVCaptureDevice failed: AVMediaTypeVideo]; // create an input stream ** input = [AVCaptureDeviceInput deviceInputWithDevice: device error: nil]; // create the output stream AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc] init]; // set the proxy to refresh [output setMetadataObjectsDelegate: self queue: dispatch_get_main_queue () // initialize the link object session = [[AVCaptureSession alloc] init]; // high quality collection rate [session setSessionPreset: AVCaptureSessionPresetHigh]; [session addInput: input]; [session addOutput: output]; // set the supported encoding formats (the bar code and QR code are compatible as follows) output. metadataObjectTypes = @ [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]; required * layer = [Allow layerWithSession: session]; layer. videoGravity = AVLayerVideoGravityResizeAspectFill; layer. frame = CGRectMake (SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, VIEW_WIDTH-2 * SCANVIEW_EdgeLeft, 220); [AVCapView. layer insertSublayer: layer atIndex: 0]; // starts to capture [session startRunning];

5. Implement timer and baseline Rolling Methods

-(Void) createTimer {_ timer = [NSTimer scheduledTimerWithTimeInterval: 1.1 target: self selector: @ selector (moveUpAndDownLine) userInfo: nil repeats: YES];}-(void) stopTimer {if ([_ timer isValid] = YES) {[_ timer invalidate]; _ timer = nil ;}// roll to: D: d-(void) moveUpAndDownLine {CGFloat YY = _ QrCodeline. frame. origin. y; if (YY! = VIEW_WIDTH-2 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop) {[UIView animateWithDuration: 1.2 animations: ^ {_ QrCodeline. frame = CGRectMake (rows + 1, VIEW_WIDTH-2 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop, VIEW_WIDTH-2 * Rows-1, 2);}];} else {[UIView animateWithDuration: 1.2 animations: ^ {_ QrCodeline. frame = CGRectMake (SCANVIEW_EdgeLeft + 1, SCANVIEW_EdgeTop, VIEW_WIDTH-2 * SCANVIEW_EdgeLeft-1, 2);}] ;}}

6. What do you do after the scan is successful? implement it in the proxy method.

-(Void) captureOutput :( AVCaptureOutput *) captureOutput didOutputMetadataObjects :( NSArray *) metadataObjects fromConnection :( AVCaptureConnection *) connection {if (metadataObjects. count> 0) {// [session stopRunning]; AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex: 0]; // output scan string NSLog (@ "% @", metadataObject. stringValue); [session stopRunning]; [self stopTimer]; [AVCapView removeFromSuperview]; ......}

 

7. If you do not want to scan, click the cancel button:

-(Void) touchAVCancelBtn {[session stopRunning]; // The camera must also be stopped [self stopTimer]; // The timer must be stopped [AVCapView removeFromSuperview]; // remove the newly created view ...}

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.