在 captureOutput 我在样本缓冲区中获取视频帧,我想在捕获的帧之上混合另一个 CIImage 。渲染应该直接进入传递的样本缓冲区。目前我使用这个:
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
let contextImage: CIImage? = CIImage(cvPixelBuffer: imageBuffer!)
let contextExtent = contextImage!.extent
// init rendering into CI context
if (m_eaglContext != EAGLContext.current()) {
EAGLContext.setCurrent(m_eaglContext)
}
m_videoPreviewView.bindDrawable()
// render overlay into pixel buffer
m_ciContext.render(m_overlayImage!, to: imageBuffer!, bounds: CGRect(x:0, y:0, width:200, height:200), colorSpace: m_colorspace)
// display the pixelbuffer into GLKView
m_ciContext.draw(contextImage!, in: m_videoPreviewViewBounds, from: contextExtent)
m_videoPreviewView.display()
}
基本上代码可以工作,但有两个问题:
m_overlayImage 的 alpha 分量被忽略,即 m_overlayImage 的半透明部分被渲染函数渲染为黑色。为什么 ?如何解决?
有什么方法可以将 m_overlayImage 绘制到 sampleBuffer 中的某个像素位置。目前我只能画到(0,0),即左下角。问题是根据文档,“边界”被应用于覆盖图像而不是样本缓冲区。如何将我的 m_overlayImage 渲染到样本缓冲区中的某个位置?
谢谢克里斯