Swift AVFoundation 二维码扫描和生成

前端之家收集整理的这篇文章主要介绍了Swift AVFoundation 二维码扫描和生成前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

项目终于不需要支持iOS6了(泪崩),在二维码扫描这一块,能够完全的放弃ZXing库,改用系统的AVFoundation了,拿swift写了个Demo,效果如下:
github地址:点这里

有关AVFoundationCore 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:"确定",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这个框架特别的强大,也可以用它来写自定义相机,拍照和录制视频等

原文链接:https://www.f2er.com/swift/326819.html

猜你在找的Swift相关文章