SWIFT Scan QRCode

來源:互聯網
上載者:User

標籤:

SWIFT中掃描QRCode代碼如下,照著敲一次再看下API的注釋應該就沒問題了。

import UIKitimport Foundationimport AVFoundationclass ViewController: UIViewController,AVCaptureMetadataOutputObjectsDelegate,UIAlertViewDelegate {    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)    let session = AVCaptureSession()    var layer:AVCaptureVideoPreviewLayer?        override func viewDidLoad() {        super.viewDidLoad()        self.view.backgroundColor = UIColor.grayColor()        let lblIntroduction = UILabel(frame: CGRectMake(10, 80, 300, 50))        lblIntroduction.text = "Scan QRCode"        lblIntroduction.textColor = UIColor.whiteColor()        self.view.addSubview(lblIntroduction)                //let imageView = UIImageView(frame: CGRectMake(10, 140, 300, 300))        //imageView.image = UIImage(named: "pick_bg")        //self.view.addSubview(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 output self.session.sessionPreset = AVCaptureSessionPresetMedium //AVCaptureSessionPresetHigh var error:NSError? //This step is to ask device where it can use back camera let input = AVCaptureDeviceInput(device: device, error: &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(20, 150, 280, 280) self.view.layer.insertSublayer(self.layer!, atIndex: 0) let output = AVCaptureMetadataOutput() output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue()) if session.canAddOutput(output) { session.addOutput(output) output.metadataObjectTypes = [AVMetadataObjectTypeQRCode] } session.startRunning() } func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { var stringValue:String? if metadataObjects.count > 0 { var metadataObject = metadataObjects[0] as! AVMetadataMachineReadableCodeObject stringValue = metadataObject.stringValue } self.session.stopRunning() let alertView = UIAlertView() alertView.message = stringValue alertView.addButtonWithTitle("Sure") alertView.delegate = self alertView.cancelButtonIndex = 0 alertView.show() } func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { session.startRunning() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}

 

 

refer to :https://github.com/hitourlee/Swift_QRCode

SWIFT Scan QRCode

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.