当我使用
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!)
时,我需要向sampleBuffer添加透明图像,但我总是得到黑色背景,代码在这里:
let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let eagleContext = EAGLContext(api: .openGLES3)
let ciContex = CIContext.init(eaglContext: eagleContext!, options: [kCIContextWorkingColorSpace:NSNull()])
let filterdImage = CIImage(cgImage: #imageLiteral(resourceName: "att_id").cgImage!)
let colorSpace = CGColorSpaceCreateDeviceRGB()
ciContex.render(filterdImage, to: pixelBuffer!, bounds: filterdImage.extent, colorSpace: colorSpace)
CVPixelBufferUnlockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
videoWriterInput.append(sampleBuffer)
我想知道如何获得背景清晰颜色。
注意: 这个答案假设您有一个背景,您想要在其上使用透明部分渲染图像。
我没有找到让 CIContext 放弃其基本黑色背景的方法,因此我设法解决此问题的唯一方法是创建两层:
CIColor.white
,但它可以是你的背景视频帧,任何东西,没有透明度)这意味着最终的上下文渲染将如下所示:
let base = CIImage(color: .white) // or some other CIImage which covers the whole frame.
let finalImage = imageWithTransparency.composited(over: base)
context.render(finalImage,
to: somePixelBuffer,
bounds: finalImage.extent,
colorSpace: someColorSpaceMatchingThePixelBuffer)