Chiper - 實時GLKView的應(yīng)用

實時濾鏡這個功能,對于一款濾鏡App來說是很重要的,如果沒有實時濾鏡,濾鏡的效果其實會大打折扣的。

要在Core Image下實現(xiàn)實時濾鏡,暫時只有使用Open GL ES的方法,而GLKView則是實現(xiàn)Open GL ES的一個View子類,所以實時濾鏡效果可以通過GLKView來實現(xiàn)。

實時濾鏡主要涉及相機(jī)圖像獲取,濾鏡實時渲染等內(nèi)容

AVCaptureSession和相機(jī)圖像獲取

設(shè)置共享的AVCaptureSession
fileprivate var avSession: AVCaptureSession = AVCaptureSession()

設(shè)置后置攝像頭為輸入源

            var cameraInput: AVCaptureDeviceInput
            guard (inputs?.count)! > 0 else {
                let _cameraDevice = AVCaptureDevice.defaultDevice(withDeviceType: AVCaptureDeviceType.builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.back)
                cameraInput = try AVCaptureDeviceInput.init(device: _cameraDevice!)
                if avSession.canAddInput(cameraInput) {
                    avSession.addInput(cameraInput)
                    avSession.sessionPreset = backPreset
                }
                currentCamera = _cameraDevice
            }

設(shè)置輸出源

            ws.captureOutput.setSampleBufferDelegate(self, queue: ws.sessionQueue)
            if ws.avSession.canAddOutput(ws.captureOutput) {
                ws.avSession.addOutput(ws.captureOutput)
            }

濾鏡實時渲染

設(shè)置完輸出源后,可以對設(shè)備輸出進(jìn)行實時渲染

    // AVCaptureVideoDataOutputSampleBufferDelegate

    func captureOutput(_ output: AVCaptureOutput, didOutputSampleBuffer sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        unowned let ws = self
        if Thread.current != Thread.main {
            DispatchQueue.main.async {
            
                connection.videoOrientation = .portrait
                let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
                let image = CIImage(cvImageBuffer: pixelBuffer!)
                if EAGLContext.current() != ws.glContext {
                    glFlush()
                    EAGLContext.setCurrent(ws.glContext)
                }
                // clear the pre content buffer
                glClearColor(0, 0, 0, 1.0)
                glClear(GLbitfield(GL_COLOR_BUFFER_BIT))
                // set the blend mode to "source over" so that CI will use that
                glEnable(GLenum(GL_BLEND))
                glBlendFunc(GLenum(GL_ONE), GLenum(GL_ONE_MINUS_SRC_ALPHA))

                let scale = UIScreen.main.bounds.size.width > 375 ? CGAffineTransform(scaleX: 3, y: 3) : CGAffineTransform(scaleX: 2, y: 2)
                ws.glkView.bindDrawable()
                ws.currentCIImage = image
                let fromRect = ws.cropImageRect(image: image)
                let filterImage = YKFilters.shared.filterCIImage(name: ws.currentFilterId, inputImage: image, inputContext: ["extent":fromRect])
                ws.ciContext.draw(filterImage, in: ws.glkView.bounds.applying(scale), from: fromRect)
                ws.glkView.display()

            }
        }

    }

1、這里有個要注意的問題,就是要對Buffer中的圖像數(shù)據(jù)的每一張圖像進(jìn)行裁剪,因為buffer獲取的是全屏幕尺寸的數(shù)據(jù),與GLKView顯示的尺寸其實是對不上的,所以要進(jìn)行裁剪。
2、除了這個問題外,GLKView要注意Context的綁定和復(fù)用,不要重復(fù)創(chuàng)建Context,否則性能會有較大的影響。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容