1.系统方式
二维码扫描
扫描只有在真机上可以测试
import UIKit
//系统扫描需要AVFoundation框架的支持
import AVFoundation
class ViewController: UIViewController,AVCaptureMetadataOutputObjectsDelegate {
//拍摄绘画
var captureSession: AVCaptureSession!
//显示图层
var previewLayer: AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//开始扫描
@IBAction func startCuptureAction(sender: UIButton) {
//调用输入设备(摄像头)
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
//输入输出信息
//输入:主要为了检测是否有设备
do {
let input = try AVCaptureDeviceInput(device: device)
//输出:主要是为了获取扫描结果
let output = AVCaptureMetadataOutput()
//设置代理(设置队列为主队列是为了扫描到结果后可以立马在主队列中秀按时出来)
//需要遵循AVCaptureMetadataOutputObjectsDelegate协议
output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
//初始化会话:实现扫描的核心类
self.captureSession = AVCaptureSession()
//将输入输出添加到会话中
self.captureSession.addInput(input)
self.captureSession.addOutput(output)
//创建显示图层
self.previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
//设置大小
self.previewLayer.frame = self.view.bounds
self.view.layer.addSublayer(self.previewLayer)
//开启会话
self.captureSession.startRunning()
}catch{
print("检测不到设备")
}
}
//协议方法
//扫描完成后的回调
//参数:扫描结果,返回的是数组
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
//停止会话
self.captureSession.stopRunning()
//移除显示图层
self.previewLayer.removeFromSuperlayer()
//获取结果
//metadataObjects只会有一个结果
if metadataObjects.count > 0 {
let object = metadataObjects.first as! AVMetadataMachineReadableCodeObject
print(object.stringValue)
}
}
}
二维码生成
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//创建滤镜处理器
//CIQRCodeGenerator:系统的Key值
let filter = CIFilter(name: "CIQRCodeGenerator")
//设置二维码携带信息
let str = "http://www.baidu.com"
//将信息设置给filter--数据类型为data
//inputMessage:系统Key值
let data = str.dataUsingEncoding(NSUTF8StringEncoding)
filter?.setValue(data, forKey: "inputMessage")
//获取信息
let outputImage = filter?.outputImage//CIImage类型
//转化图片
let image = UIImage(CIImage: outputImage!)
//显示
let imageView = UIImageView(frame: CGRectMake(0,0,200,200))
imageView.image = image
self.view.addSubview(imageView)
}
}
2.使用第三方库ZBarSDK
步骤:
1.拖入第三方库并桥接
2.静态库
实现
import UIKit
//常用的关于二维码生成和扫描的第三方包括ZBarSDK和ZXingSDK,由于ZXingSDK集成比较复杂,并且已经停止更新。ZBarSDK封装自系统CIFilter(滤镜处理器)和AVCaptureSession
class ViewController: UIViewController {
@IBOutlet weak var erWeiMaImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
//二维码生成
//参数1:二维码携带信息
//参数2:二维码的清晰度(0-500),数值越大越清晰
erWeiMaImageView.image = QRCodeGenerator.qrImageForString("http://www.baidu.com", imageSize: 400)
}
//扫描
@IBAction func captureAction(sender: UIButton) {
//参数1:如果true,表示关闭二维码扫描,只扫描条形码
//闭包参数1:扫描结果
//参数2:是否扫描成功
let customVC = CustomViewController(isQRCode: false) { (result, isSuccess) -> Void in
if isSuccess{
print("扫描成功\(result)")
//在实际开发中扫描成功后一般会跳转页面,将result进行传值
}
else{
print("扫描失败")
}
}
self.presentViewController(customVC, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}