Swift AVFoundation 二维码扫描和生成
2017-08-04 19:26 tlnshuju 阅读(599) 评论(0) 编辑 收藏 举报项目最终不须要支持iOS6了(泪崩),在二维码扫描这一块,可以全然的放弃ZXing
库,改用系统的AVFoundation
了,拿swift
写了个Demo,效果例如以下:
github地址:点这里
有关AVFoundation
和Core Image
(滤镜等),可以先看看objc.io第21期和第23期的有关介绍.
初始化视频捕捉
// 初始化视频捕获
private func initCapture() {
// 代表抽象的硬件设备,这里传入video
let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
var error: NSError?
// 输入流
var captureInput = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &error) as?
AVCaptureDeviceInput
if (error != nil && captureInput == nil) {
let errorAlert = UIAlertController(title: "提醒", message: "请在iPhone的\"设置-隐私-相机\"选项中,同意XXX訪问您的相机", preferredStyle: .Alert)
errorAlert.addAction(UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(errorAlert, animated: true, completion: nil)
} else {
// input和output的桥梁,它协调着intput到output的传输数据.(见字意,session-会话)
captureSession = AVCaptureSession()
captureSession!.addInput(captureInput)
// 输出流
let captureMetadataOutput = AVCaptureMetadataOutput()
// 限制扫描区域http://blog.csdn.net/lc_obj/article/details/41549469
captureMetadataOutput.rectOfInterest = CGRectMake(128.0/ScreenWH.screenHeight, (ScreenWH.screenWidth - 280.0)/ScreenWH.screenWidth * 2.0, 280.0/ScreenWH.screenHeight, 280.0/ScreenWH.screenWidth)
captureSession!.addOutput(captureMetadataOutput)
// 加入的队列按规定必须是串行
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
// 指定信息类型,QRCode,你懂的
captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
// 用这个预览图层和图像信息捕获会话(session)来显示视频
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
videoPreviewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer!.frame = view.bounds
view.layer.addSublayer(videoPreviewLayer!)
}
}
PS:LZ用了下微信和新浪微博的扫一扫,发现那个扫描框是忽悠人的,也就是你没拿它对准二维码,仅仅要二维码进入手机摄像头范围,就行解码成功….囧
所以LZ在代码中做了一个扫描区域的限制(感觉蛮无聊的)
实现代理解码
// MARK: - AVCaptureMetadataOutputObjectsDelegate
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
if metadataObjects == nil || metadataObjects.count == 0 {
captureView!.frame = CGRectZero
return
}
// 刷取出来的数据
for metadataObject in metadataObjects {
if metadataObject.type == AVMetadataObjectTypeQRCode {
let metadata = metadataObject as! AVMetadataMachineReadableCodeObject
// 元数据对象就会被转化成图层的坐标
let codeCoord = videoPreviewLayer!.transformedMetadataObjectForMetadataObject(metadata) as! AVMetadataMachineReadableCodeObject
captureView!.frame = codeCoord.bounds
if metadata.stringValue != nil {
println("\(metadata.stringValue)")
self.captureSession!.stopRunning()
let successAlert = UIAlertController(title:"提示", message:"是否打开" + metadata.stringValue, preferredStyle: .Alert)
successAlert.addAction(UIAlertAction(title:"取消", style: .Default, handler: { (_) -> Void in
self.stopCapture()
}))
successAlert.addAction(UIAlertAction(title:"确定", style: .Default, handler: { (_) -> Void in
if metadata.stringValue.lowercaseString.hasPrefix("http") {
UIApplication.sharedApplication().openURL(NSURL(string: metadata.stringValue)!)
self.navigationController!.popViewControllerAnimated(true)
}
}))
self.presentViewController(successAlert, animated: true, completion: nil)
}
}
}
}
数据转换AVMetadataMachineReadableCodeObject
相应二维码.
生成二维码
// MARK: - Private Methods
private func createQRForString(qrString: String?, qrImageName: String?) -> UIImage?{
if let sureQRString = qrString {
let stringData = sureQRString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
// 创建一个二维码的滤镜
let qrFilter = CIFilter(name: "CIQRCodeGenerator")
qrFilter.setValue(stringData, forKey: "inputMessage")
qrFilter.setValue("H", forKey: "inputCorrectionLevel")
let qrCIImage = qrFilter.outputImage
// 创建一个颜色滤镜,黑白色
let colorFilter = CIFilter(name: "CIFalseColor")
colorFilter.setDefaults()
colorFilter.setValue(qrCIImage, forKey: "inputImage")
colorFilter.setValue(CIColor(red: 0, green: 0, blue: 0), forKey: "inputColor0")
colorFilter.setValue(CIColor(red: 1, green: 1, blue: 1), forKey: "inputColor1")
// 返回二维码image
let codeImage = UIImage(CIImage: colorFilter.outputImage.imageByApplyingTransform(CGAffineTransformMakeScale(5, 5)))
// 通常,二维码都是定制的,中间都会放想要表达意思的图片
if let iconImage = UIImage(named: qrImageName!) {
let rect = CGRectMake(0, 0, codeImage!.size.width, codeImage!.size.height)
UIGraphicsBeginImageContext(rect.size)
codeImage!.drawInRect(rect)
let avatarSize = CGSizeMake(rect.size.width * 0.25, rect.size.height * 0.25)
let x = (rect.width - avatarSize.width) * 0.5
let y = (rect.height - avatarSize.height) * 0.5
iconImage.drawInRect(CGRectMake(x, y, avatarSize.width, avatarSize.height))
let resultImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resultImage
}
return codeImage
}
return nil
}
如图
结尾:AVFoundation
这个框架特别的强大,也可以用它来写自己定义相机,拍照和录制视频等