【问题标题】:Core Image - rendering a transparent image on CMSampleBufferRef result in black box around it核心图像 - 在 CMSampleBufferRef 上渲染透明图像会导致其周围出现黑框
【发布时间】:2014-09-09 17:13:19
【问题描述】:

我正在尝试在使用 AVFoundation 的 AVCaptureVideoDataOutput 录制的视频上添加水印/徽标。 我的类设置为 sampleBufferDelegate 并接收 CMSamplebufferRefs。我已经对 CMSampleBufferRefs CVPixelBuffer 应用了一些效果,并将其传递回 AVAssetWriter。

左上角的徽标是使用透明 PNG 交付的。 我遇到的问题是 UIImage 的透明部分在写入视频后是黑色的。 任何人都知道我做错了什么或可能忘记了什么?

下面的代码sn-ps:

//somewhere in the init of the class;   
 _eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
_ciContext = [CIContext contextWithEAGLContext:_eaglContext
                                       options: @{ kCIContextWorkingColorSpace : [NSNull null] }];

//samplebufferdelegate method:
- (void) captureOutput:(AVCaptureOutput *)captureOutput
 didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
        fromConnection:(AVCaptureConnection *)connection {

    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(pixelBuffer, 0);

....

UIImage *logoImage = [UIImage imageNamed:@"logo.png"];
CIImage *renderImage = [[CIImage alloc] initWithCGImage:logoImage.CGImage];
CGColorSpaceRef cSpace = CGColorSpaceCreateDeviceRGB();

[_ciContext render:renderImage
   toCVPixelBuffer:pixelBuffer
            bounds: [renderImage extent]
        colorSpace:cSpace];

CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CGColorSpaceRelease(cSpace);

....
}

看起来 CIContext 没有绘制 CIImages alpha。 有什么想法吗?

【问题讨论】:

    标签: ios avfoundation core-image cmsamplebufferref


    【解决方案1】:

    对于遇到相同问题的开发者:

    似乎在 GPU 上渲染并写入视频的任何内容最终都会在视频中形成一个黑洞。 相反,我删除了上面的代码,创建了一个 CGContextRef,就像您在编辑图像时所做的那样,并在该上下文上进行绘制。

    代码:

    ....
    CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
    
    CGContextRef context = CGBitmapContextCreate(CVPixelBufferGetBaseAddress(pixelBuffer),
                                                 CVPixelBufferGetWidth(pixelBuffer),
                                                 CVPixelBufferGetHeight(pixelBuffer),
                                                 8,
                                                 CVPixelBufferGetBytesPerRow(pixelBuffer),
                                                 CGColorSpaceCreateDeviceRGB(),
                                                 (CGBitmapInfo)
                                                 kCGBitmapByteOrder32Little |
                                                 kCGImageAlphaPremultipliedFirst);
    
    CGRect renderBounds = ...
    CGContextDrawImage(context, renderBounds, [overlayImage CGImage]);
    
    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
    CGColorSpaceRelease(cSpace);
    ....
    

    当然,不再需要全局 EAGLContextCIContext

    【讨论】:

    • 你可以链接到一些示例代码。我非常了解发生了什么,但这有点超出我的想象。尝试在录制时为视频添加水印。所以这正是我所需要的。
    • @chrisallick 此代码进入 AVCaptureVideoDataOutput 的 sampleBufferDelegate 的 captureOutput:didOutputSampleBuffer:fromConnection:-method。文档:developer.apple.com/Library/ios/documentation/AVFoundation/…
    • 它对我不起作用。我将清理我创建的示例并分享它。
    猜你喜欢
    • 2013-02-08
    • 1970-01-01
    • 2011-09-08
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 2020-08-13
    • 2016-12-26
    相关资源
    最近更新 更多