Implementation of the QR code scanning function for ios7

Source: Internet
Author: User

Before ios7, we developed two-dimensional code scanning or production, which must be developed using third-party open-source libraries.

Then, when upgraded to ios7, Apple provided the QR code scanning function in passbook, and the scanning speed was very fast, killing all third-party open-source libraries in seconds. Therefore, the development and selection of QR codes should have a higher sdk priority than other libraries.


When talking about QR code development, we need to use these two objects. As follows ..

@ Property (nonatomic, strong) AVCaptureSession * captureSession;

@ Property (nonatomic, strong) AVCaptureVideoPreviewLayer * videoPreviewLayer;


CaptureSession this is a capture session, that is, you can use this object to capture data streams from the input device.

AVCaptureVideoPreviewLayer displays captured data streams through the output device.

First, we should determine whether the current device has a device that captures data streams.

AVCaptureDevice * captureDevice = [avcapturedevicedefadevicdevicewithmediatype: AVMediaTypeVideo];

AVCaptureDeviceInput * input = [avcapturedevicdevicputdeviceinputwithdevice: captureDeviceerror: & error];

If (! Input ){

NSLog (@ "% @", [errorlocalizedDescription]);

Return NO;

}

As shown above, if an error is caught, it is returned directly.

Initialize a CaptureSession object

_ CaptureSession = [[AVCaptureSessionalloc] init];

Set the input device of the session

[_ CaptureSession addInput: input];


Corresponding output

AVCaptureMetadataOutput * captureMetadataOutput = [[AVCaptureMetadataOutputalloc] init];

[_ CaptureSession addOutput: captureMetadataOutput];


Create a queue

Dispatch_queue_t dispatchQueue;

DispatchQueue = dispatch_queue_create ("myQueue", NULL );

[CaptureMetadataOutput setMetadataObjectsDelegate: selfqueue: dispatchQueue];

[CaptureMetadataOutput setMetadataObjectTypes: [NSArrayarrayWithObject: AVMetadataObjectTypeQRCode];


The downcaptured data streams are displayed.

_ VideoPreviewLayer = [[AVCaptureVideoPreviewLayeralloc] initWithSession: _ captureSession];

[_ VideoPreviewLayersetVideoGravity: AVLayerVideoGravityResizeAspectFill];

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

[_ ViewPreview. layeraddSublayer: _ videoPreviewLayer];

Start capture

[_ CaptureSession startRunning];



The obtained data is stored in

AVCaptureMetadataOutputObjectsDelegate:

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


Determine whether there is data or whether there is QR code data

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

AVMetadataMachineReadableCodeObject * metadataObj = [metadataObjectsobjectAtIndex: 0];

If ([[metadataObjtype] isEqualToString: AVMetadataObjectTypeQRCode]) {

// Obtain the scanned data and end the scan

[Self?mselec=mainthread: @ selector (stopReading :) withObject: metadataObj. stringValuewaitUntilDone: NO];


}


}




One small step every day, hoping to help some friends.


Good night ....









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.