Swift AVFoundation 二維碼掃描和產生

來源:互聯網
上載者:User

標籤:ios   swift   二維碼   

項目終於不需要支援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:"確定", 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這個架構特別的強大,也可以用它來寫自訂相機,拍照和錄製視頻等

Swift AVFoundation 二維碼掃描和產生

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.