iOS: QR Code scan

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>

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.