SWIFT Scan QRCode

Source: Internet
Author: User

In swift, scan the QRCode code as follows, and then look at the API's comments as soon as you tap it.

Import Uikitimport foundationimport Avfoundationclass Viewcontroller:uiviewcontroller, avcapturemetadataoutputobjectsdelegate,uialertviewdelegate {Let device = Avcapturedevice.defaultdevicewithmediatype (Avmediatypevideo) Let session = Avcapturesession () var layer:avcapturevid        Eopreviewlayer?  Override Func Viewdidload () {super.viewdidload () Self.view.backgroundColor = Uicolor.graycolor () let Lblintroduction = UILabel (Frame:cgrectmake (ten, +,)) Lblintroduction.text = "Scan qrcode" Lblintro Duction.textcolor = Uicolor.whitecolor () self.view.addSubview (lblintroduction)//let ImageView = UII Mageview (frame:cgrectmake)//imageview.image = UIImage (named: "PICK_BG")//self.view.add Subview (ImageView)
Add scan Line Effect
Let line = UIView (Frame:cgrectmake (20, 150, 280, 1))
Line.backgroundcolor = Uicolor.yellowcolor ()
Self.view.addSubview (line)

Uiview.animatewithduration (2.5, delay:0, options:UIViewAnimationOptions.Repeat, animations: {()-Void in
Line.frame = CGRectMake (20, 430, 280, 1)
}, Completion:nil)

Setupcamera ()} func Setupcamera () {//an avcapturesession preset suitable for medium quality out Put self.session.sessionPreset = Avcapturesessionpresetmedium//avcapturesessionpresethigh var error:nserror ? This is the to ask device where it can use the back camera let input = Avcapturedeviceinput (Device:device, Error: & AMP;ERROR) if Error! = Nil {println (error?. Description) return} if Session.canaddinput (input) {session.addinput (input) } layer = Avcapturevideopreviewlayer (session:session) layer?. Videogravity = Avlayervideogravityresizeaspectfill layer?. frame = CGRectMake (280, 280) Self.view.layer.insertSublayer (self.layer!, atindex:0) let OU Tput = Avcapturemetadataoutput () output.setmetadataobjectsdelegate (self, queue:dispatch_get_main_queue ()) I F session.canaddoutput (Output) { Session.addoutput (output) output.metadataobjecttypes = [Avmetadataobjecttypeqrcode]} Session.startrunning ()} func captureoutput (captureoutput:avcaptureoutput!, Didoutputmetadataobject s metadataobjects: [anyobject]!, Fromconnection connection:avcaptureconnection!) {var stringvalue:string? If Metadataobjects.count > 0 {var metadataobject = metadataobjects[0] as! Avmetadatamachinereadablecodeobject stringvalue = metadataobject.stringvalue} Self.sessi On.stoprunning () Let Alertview = Uialertview () alertview.message = StringValue Alertview.addbuttonwit Htitle ("sure") alertview.delegate = self Alertview.cancelbuttonindex = 0 alertview.show ()} Func Alertview (Alertview:uialertview, Clickedbuttonatindex buttonindex:int) {session.startrunning ()} ove Rride func didreceivememorywarning () {Super.didreCeivememorywarning ()//Dispose of any of the resources that can is recreated. }}

Refer To:https://github.com/hitourlee/swift_qrcode

SWIFT Scan QRCode

Related Article

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.