iOS Swift Barcode Reading -


i have barcode scanning working in app. after barcode detected stop capture session allow processing of barcode. however, after barcode processed want scanning controller stay , next barcode scanned. had assumed starting capture session (startrunning()) image stays frozen. how can start capture session again?

to stop session use code

            self.session.stoprunning() 

to begin agian, use code

    self.session.startrunning() 

here code implement barcode scanner...

import uikit import avfoundation   class viewcontroller: uiviewcontroller, avcapturemetadataoutputobjectsdelegate {      let session         : avcapturesession = avcapturesession()     var previewlayer    : avcapturevideopreviewlayer!     var highlightview   : uiview = uiview()      override func viewdidload() {         super.viewdidload()          // allow view resize freely         self.highlightview.autoresizingmask =   uiviewautoresizing.flexibletopmargin |             uiviewautoresizing.flexiblebottommargin |             uiviewautoresizing.flexibleleftmargin |             uiviewautoresizing.flexiblerightmargin          // select color want completed scan reticle         self.highlightview.layer.bordercolor = uicolor.greencolor().cgcolor         self.highlightview.layer.borderwidth = 3          // add our controller's view subview.         self.view.addsubview(self.highlightview)           // sake of discussion camera         let device = avcapturedevice.defaultdevicewithmediatype(avmediatypevideo)          // create nilable nserror hand off next method.         // make sure use "var" keyword , not "let"         var error : nserror? = nil           let input : avcapturedeviceinput? = avcapturedeviceinput.deviceinputwithdevice(device, error: &error) as? avcapturedeviceinput          // if our input not nil add session, otherwise we're kind of done!         if input != nil {             session.addinput(input)         }         else {             // fine demo, real in app. :)             println(error)         }          let output = avcapturemetadataoutput()         output.setmetadataobjectsdelegate(self, queue: dispatch_get_main_queue())         session.addoutput(output)         output.metadataobjecttypes = output.availablemetadataobjecttypes           previewlayer = avcapturevideopreviewlayer.layerwithsession(session) as! avcapturevideopreviewlayer         previewlayer.frame = self.view.bounds         previewlayer.videogravity = avlayervideogravityresizeaspectfill         self.view.layer.addsublayer(previewlayer)          // start scanner. you'll have end later.         session.startrunning()      }      // called when find known barcode type camera.     func captureoutput(captureoutput: avcaptureoutput!, didoutputmetadataobjects metadataobjects: [anyobject]!, fromconnection connection: avcaptureconnection!) {          var highlightviewrect = cgrectzero          var barcodeobject : avmetadataobject!          var detectionstring : string!          let barcodetypes = [avmetadataobjecttypeupcecode,             avmetadataobjecttypecode39code,             avmetadataobjecttypecode39mod43code,             avmetadataobjecttypeean13code,             avmetadataobjecttypeean8code,             avmetadataobjecttypecode93code,             avmetadataobjecttypecode128code,             avmetadataobjecttypepdf417code,             avmetadataobjecttypeqrcode,             avmetadataobjecttypeazteccode         ]           // scanner capable of capturing multiple 2-dimensional barcodes in 1 scan.         metadata in metadataobjects {              barcodetype in barcodetypes {                  if metadata.type == barcodetype {                     barcodeobject = self.previewlayer.transformedmetadataobjectformetadataobject(metadata as! avmetadatamachinereadablecodeobject)                      highlightviewrect = barcodeobject.bounds                      detectionstring = (metadata as! avmetadatamachinereadablecodeobject).stringvalue                      self.session.stoprunning()  self.alert(detectionstring)                     break                 }              }         }          println(detectionstring)         self.highlightview.frame = highlightviewrect         self.view.bringsubviewtofront(self.highlightview)      }        func alert(code: string){         let actionsheet:uialertcontroller = uialertcontroller(title: "barcode", message: "\(code)", preferredstyle: uialertcontrollerstyle.alert)          // alert add .alert instead of .action sheet           // start copy          let firstalertaction:uialertaction = uialertaction(title: "ok", style: uialertactionstyle.default, handler:               {                 (alertaction:uialertaction!) in                   // action when pressed                  self.session.startrunning()             })          actionsheet.addaction(firstalertaction)          // end copy               self.presentviewcontroller(actionsheet, animated: true, completion: nil)      }    } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -