SWIFT Scan QRCode
2015-08-15 21:59
579 查看
SWIFT中扫描QRCode代码如下,照着敲一次再看下API的注释应该就没问题了。
refer to :https://github.com/hitourlee/Swift_QRCode
import UIKit import Foundation import AVFoundation class 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 Optional Type(问号?和感叹号!)的理解和使用
- Swift学习笔记系列——(21)协议
- swift 中,protocol的optional方法
- Watch OS 2 Complication 开发教程 (二) (持续连载中)
- swift- tableView 的使用
- swift-tabbar的使用,页面之间的跳转
- Swift-常量(let)和变量(var)
- Swift-入门简介
- swift中使用GCDMulticastDelegate
- swift--闭包
- Swift 字典的常用方法
- swift简介
- Swift:什么时候使用结构体和类
- 基于Swift语言开发微信、QQ和微博的SSO授权登录代码分析
- Swift 数组的常用操作
- Swift String 一些常用方法
- Swift 2.0 项目中利用中自定义行高 时遇到的问题?
- Swift学习——Swift解释特定的基础(七)
- Swift和OC混用
- Chapter 17 Core Data iOS 8 -Swift Programming cookBook 读书笔记