iOS: QR Code scan
Last Update:2016-01-11
Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p><p><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">IOS QR code scanning borrowing #import <AVFoundation/AVFoundation.h> implementation, will use <AVCaptureMetadataOutputObjectsDelegate> this agent , and the camera will be called to scan,</span></p></p><p><p><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">The specific implementation steps are as Follows:</span></p></p><p><p><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"></span></p></p><p><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">//1. Create a capture session</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">avcapturesession *session = [[avcapturesession alloc] init];</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"> </span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">//2. Add an input source</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">avcapturedevice *device = [avcapturedevice defaultdevicewithmediatype:avmediatypevideo];</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">avcapturedeviceinput *input = [avcapturedeviceinput deviceinputwithdevice:device error:nil];</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">[session addinput:input];</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">self.session = session;</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"> </span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">//3. Add an output source</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">avcapturemetadataoutput *output = [[avcapturemetadataoutput alloc] init];</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">[output setmetadataobjectsdelegate:self queue:dispatch_get_main_queue ()];</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">[session addoutput:output];</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">[output setmetadataobjecttypes:@[avmetadataobjecttypeqrcode]];</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"> </span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">//4. Add a preview layer</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">Avcapturevideopreviewlayer *layer = [avcapturevideopreviewlayer layerwithsession:session];</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">layer.frame = self.view.bounds;</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">[self.view.layer addsublayer:layer];</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">Self.layer = layer;</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"> </span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">//5. turn on scan</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">[session startrunning];</span></span></p><p><p><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"></span></p></p><p><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">#pragma mark-proxy method for implementing an output source</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">-(void) captureoutput: (avcaptureoutput *) captureoutput didoutputmetadataobjects: (nsarray *) metadataobjects Fromconnection: (avcaptureconnection *) Connection</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">{</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">if (metadataobjects.count > 0) {</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">avmetadatamachinereadablecodeobject *object = [metadataobjects lastobject];</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">NSLog (@ "%@", object.stringvalue);</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"> </span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">[self.session stoprunning];</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">[self.layer removefromsuperlayer];</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">} else {</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">NSLog (@ "failed to get data");</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"> }</span></span><br><span style="font-size: 14pt; font-family: ‘courier new‘, courier;"><span style="font-size: 14pt; font-family: ‘courier new‘, courier;">}</span></span></p><p><p>iOS: QR Code scan</p></p></span>