核心图像-在CMSampleBufferRef上呈现透明图像会导致其周围出现黑框 [英] Core Image - rendering a transparent image on CMSampleBufferRef result in black box around it

查看:186
本文介绍了核心图像-在CMSampleBufferRef上呈现透明图像会导致其周围出现黑框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to add a watermark/logo on a video that I'm recording using AVFoundation's AVCaptureVideoDataOutput. My class is set as the sampleBufferDelegate and receives the CMSamplebufferRefs. I already apply some effects to the CMSampleBufferRefs CVPixelBuffer and pass it back to the AVAssetWriter.

左上角的徽标是使用透明PNG交付的. 我遇到的问题是,一旦将UIImage的透明部分写入视频,它就会变成黑色. 任何人都知道我在做错什么,或者可能会忘记吗?

The logo in the top left corner is delivered using a transparent PNG. The problem I'm having is that the transparent parts of the UIImage are black once written to the video. Anyone have an idea of what I'm doing wrong or could be forgetting?

以下代码段:

//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. 有什么想法吗?

It looks like the CIContext does not draw the CIImages alpha. Any ideas?

推荐答案

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

For developers who've encountered the same issue:

似乎在GPU上渲染并写入视频的所有内容最终在视频中造成黑洞. 相反,我删除了上面的代码,像编辑图像时一样创建了CGContextRef,并绘制了该上下文.

It appears anything rendered on the GPU and written to the video ends up making a black hole in the video. Instead, I removed the above code, created a CGContextRef, like you would do when editing images, and drew on that context.

代码:

....
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.

And off course, the global EAGLContext and the CIContext are not needed anymore.

这篇关于核心图像-在CMSampleBufferRef上呈现透明图像会导致其周围出现黑框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆