//
// 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-二维码的生成
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 序言 在iOS7之前二维码扫描主要是用的第三方库,如ZXing或者ZBar。使用起来比较麻烦,出错也不容易调试。i...
- 主要介绍在Unity里调用Android原生代码,即用Androidstudio导出 aar 文件,在Unity里...