美文网首页Swift
swift-二维码的生成

swift-二维码的生成

作者: Coder东 | 来源:发表于2017-03-30 15:54 被阅读55次
//
//  QRCodeViewController.swift
//  Swift-二维码的生成
//
//  Created by 品德信息 on 2017/1/23.
/  Copyright © 2017年 品德信息. All rights reserved.
//

import UIKit
import AVFoundation

class QRCodeViewController:     UIViewController,AVCaptureMetadataOutputObjectsDelegate {

@IBOutlet weak var topView: UIView!
@IBOutlet weak var msgLabel: UILabel!

//声明变量
var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var qrCodeFrameView: UIView?

let supportedCodeTypes = [AVMetadataObjectTypeUPCECode,
                          AVMetadataObjectTypeCode39Code,
                          AVMetadataObjectTypeCode39Mod43Code,
                          AVMetadataObjectTypeCode93Code,
                          AVMetadataObjectTypeCode128Code,
                          AVMetadataObjectTypeEAN8Code,
                          AVMetadataObjectTypeEAN13Code,
                          AVMetadataObjectTypeAztecCode,
                          AVMetadataObjectTypePDF417Code,
                          AVMetadataObjectTypeQRCode]


override func viewDidLoad() {
    super.viewDidLoad()
    
    //二维码:QR(Quick Response)是有Denso开发的一种二维条形码
    //在ios中任何的条形码扫描,包括二维码扫描,都是基于视频捕捉的,这也是为什么条形码扫描功能添加在AVFoundation框架中
//二维码识别完全是依赖视频捕捉的,为了进行实时捕捉,我们需要实例化一个适当的输入设置的AVCaptureDevice的AVCaptureSession对象进行视频捕捉
    // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter.
    let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
    
    do{
        let  input = try AVCaptureDeviceInput(device: captureDevice)
         // Initialize the captureSession object.
        captureSession = AVCaptureSession()
        //set the input device on the capture session
        captureSession?.addInput(input)
        //initialize a avcapturemetadataOutput object and set it as the device to the capture session
        let captureMetadataOutput = AVCaptureMetadataOutput()
        captureSession?.addOutput(captureMetadataOutput)
        
        //set delegate and use the default dispatch queue to execute the call back
        //设置代理并使用默认的线程去执行返回
        captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        captureMetadataOutput.metadataObjectTypes = supportedCodeTypes
        
        //initialize the video preview layer and add it as a sublayer to the viewPreview view‘s layer
        videoPreviewLayer = AVCaptureVideoPreviewLayer(session:captureSession)
        videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
        videoPreviewLayer?.frame = view.layer.bounds
        view.layer.addSublayer(videoPreviewLayer!)
        
        //start video capture
        captureSession?.startRunning()
        
        
        //move the message label and top bar to the front 
        view.bringSubview(toFront: msgLabel)
        view.bringSubview(toFront: topView)
        
        
        //initialize QR Code Frame to hightlight the QR code 
        qrCodeFrameView = UIView()
        
        if let qrCodeFrameView = qrCodeFrameView{
            qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
            qrCodeFrameView.layer.borderWidth = 2
            view.addSubview(qrCodeFrameView)
            view.bringSubview(toFront: qrCodeFrameView)
        }
    } catch{
        
        //if any error occurs ,simply print it out and don't continue any more
        print(error)
        return
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


//MARK: - AVCaptureMetadataOutputObjectsDelegate  Methods
 func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
    
    
}
/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

相关文章

网友评论

    本文标题:swift-二维码的生成

    本文链接:https://www.haomeiwen.com/subject/xuhpottx.html