iOS QR Code scan implementation (SWIFT)

Source: Internet
Author: User

With the universal use of QR Code, QR code scanning has become a basic function of many apps, this article mainly introduces how to implement a simple QR code scanning function. This article uses the avfoundation that comes with Xcode

Library, implemented using Swfit language.

The steps to implement are as follows:

1. Get a video device

In QR code scanning, our input stream is video. We need the enable video device to get the appropriate metadata.

2. Create session to process the video's input and output stream

3. Create the input and output stream and add it to the session

4. Processing QR code data

This method is a member method of avcapturemetadataoutputobjectsdelegate and we need to implement it

~~~~~~~~~~~~~~~~~~~~~~~~~ I am the code:) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

viewcontroller.swift//qrcodetest////Created by Xualvin on 2/2/16.//copyright©2016 year Xualvin. All rights Reserved.//import Uikitimport Avfoundationclass Viewcontroller:uiviewcontroller,        Avcapturemetadataoutputobjectsdelegate {var session:avcapturesession? Override Func Viewdidload () {super.viewdidload ()//Get camera device, note is video instead of audio let device = AVCA Pturedevice.defaultdevicewithmediatype (Avmediatypevideo)//Initialize AV session to reconcile and process AV input and output streams let session = Avcapt Uresession ()//Create input stream let Input:avcapturedeviceinput? = try!        Avcapturedeviceinput (Device:device) if session.canaddinput (input) {session.addinput (input) }//Create output stream Let Output:avcapturemetadataoutput = Avcapturemetadataoutput () if Session.canadd Output (output) {session.addoutput (outputs)//set out stream agent, all metadata received from the receiving end will be transferred to the delegate method, all delegate methods are in the queue Execute Output.setmeta inDataobjectsdelegate (self, Queue:dispatch_get_main_queue ())//Set the type of metadata, here is the QR code qrcode Output.metadatao Bjecttypes = [Avmetadataobjecttypeqrcode]}//create video Device capture video area, in this case full screen let Layer:avcapturevideopre Viewlayer = Avcapturevideopreviewlayer.init (session:session) layer.videogravity = AVLAYERVIDEOGRAVITYRESIZEASPECTF        Ill layer.frame=self.view.layer.bounds self.view.layer.insertSublayer (layer, atindex:0)//Start capturing video data Session.startrunning ()}//Implement Avcapturemetadataoutputobjectsdelegate member method to process QR code information func C Aptureoutput (captureoutput:avcaptureoutput!, didoutputmetadataobjects metadataobjects: [AnyObject]!, Fromconnection connection:avcaptureconnection!) {session?.                Stoprunning ()//Get QR Code information metadata if let metadataobjects = metadataobjects.first{ Let Readableobject = Metadataobjects as!                 Avmetadatamachinereadablecodeobject               Add Vibration Audioservicesplaysystemsound (Systemsoundid (ksystemsoundid_vibrate))// Pop-up QR code information let Alertctl:uialertcontroller = Uialertcontroller (title: "QR Code information", message:readableObject.string Value, PreferredStyle:UIAlertControllerStyle.Alert)//Add action to Uialertcontroller,action is UI Alertcontroller on the button operation, it is recommended to add at least one, if not added, Alertcontroller will not have the corresponding button exit let Alertaction:uialertact                    Ion = Uialertaction (title: "Action", Style:UIAlertActionStyle.Default, Handler: {(param:uialertaction!), Void in Print ("I am Alert Action")}) Alertctl.addaction (Aler taction)//Show alert Controller Self.presentviewcontroller (Alertctl, Animated:true, Completio N:nil)}}}

  

iOS QR Code scan implementation (SWIFT)

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.