1. 程式人生 > >Swift AVFoundation 二維碼掃描和生成

Swift AVFoundation 二維碼掃描和生成

size ram post nserror out 捕獲 一個 白色 art

項目最終不須要支持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 二維碼掃描和生成