2014-07-04 2 views
3

AVFoundation의 AVCaptureVideoDataOutput을 사용하여 녹화중인 비디오에 워터 마크/로고를 추가하려고합니다. 내 클래스는 sampleBufferDelegate로 설정되고 CMSamplebufferRefs를받습니다. 이미 CMSampleBufferRefs CVPixelBuffer에 몇 가지 효과를 적용하고이를 AVAssetWriter에 다시 전달합니다.코어 이미지 - CMSampleBufferRef의 투명한 이미지를 블랙 박스로 렌더링합니다.

왼쪽 상단 모서리의 로고는 투명한 PNG를 사용하여 전달됩니다. 내가 가지고있는 문제는 UIImage의 투명한 부분이 검은 색으로 일단 비디오에 기록되었다는 것입니다. 누구나 내가 잘못하고 있거나 잊어 버릴 수있는 것에 대한 아이디어가 있습니까? 아래

코드 조각 :

//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 알파를 그릴하지 않습니다. 아이디어가 있으십니까? 동일한 문제가 발생했습니다 개발자의

답변

6

:

이 비디오에 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가 더 이상 필요하지 않습니다.

+0

언제든지 일부 샘플 코드에 연결할 수 있습니다. 나는 무슨 일이 일어나고 있는지 꽤 많이 이해하지만 그것은 내 머리 위로 조금있다. 녹화하는 동안 동영상 워터 마크를하려고합니다. 그래서 이것은 내가 필요한 것입니다. – chrisallick

+0

@chrisallick이 코드는 captureOutput : didOutputSampleBuffer : fromConnection : AVCaptureVideoDataOutput의 sampleBufferDelegate 메소드에서 사용됩니다. 문서 : https://developer.apple.com/Library/ios/documentation/AVFoundation/Reference/AVCaptureVideoDataOutput_Class/index.html – JoriDor

+0

저에게 적합하지 않았습니다. 내가 만든 예제를 정리하고 공유 할 것입니다. – chrisallick

관련 문제