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