zoukankan      html  css  js  c++  java
  • SWIFT Scan QRCode

    SWIFT中扫描QRCode代码如下,照着敲一次再看下API的注释应该就没问题了。

    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

  • 相关阅读:
    Android中LayoutInflater的使用
    m2014-architecture-webserver->百万记录级mysql数据库及Discuz!论坛优化
    m2014-architecture-imgserver->利用Squid反向代理搭建CDN缓存服务器加快Web访问速度
    m2014-architecture-imgserver->Lighttpd +mod_mem_cache的效果简直太好了
    m2014-architecture-imgserver->配置lighttpd mod_mem_cache 模块做静态资源服务器
    m2014-architecture-imgserver->Lighttpd Mod_Cache很简单很强大的动态缓存
    雅虎十四条
    java高新技术-基本数据类型拆装箱及享元设计模式
    java高新技术-可变参数与OverLoad相关面试题分析
    java高新技术-java5的静态导入与编译器语法设置
  • 原文地址:https://www.cnblogs.com/foxting/p/4733226.html
Copyright © 2011-2022 走看看