實時濾鏡這個功能,對于一款濾鏡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,否則性能會有較大的影響。