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 ....