2013-10-11 7 views
7

나는 AVCaptureVideoDataOutput와 함께 일하고 있으며 CMSampleBufferRefUIImage으로 변환하고 싶습니다. 많은 답변이 UIImage created from CMSampleBufferRef not displayed in UIImageView?AVCaptureSession with multiple previewsYUV 색상 공간으로 CMSampleBufferRef를 UIImage로 변환 하시겠습니까?

내가 BGRA에 VideoDataOutput 색 공간을 설정하면 그것은 잘 작동 위 videoSettings없이

NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; 
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; 
[dataOutput setVideoSettings:videoSettings]; 

(이 답변 CGBitmapContextCreateImage error 적립)처럼, 동일, 나는을 받게됩니다 BGRA에 YUV (기본 AVCaptureSession 색 공간)에서 변환 오버 헤드가 있기 때문에 브래드과 코도 난에 의해 명시된 바와 같이 다음과 같은 오류가 BGRA 작업

CGBitmapContextCreate: invalid data bytes/row: should be at least 2560 for 8 integer bits/component, 3 components, kCGImageAlphaPremultipliedFirst. 
<Error>: CGBitmapContextCreateImage: invalid context 0x0 

은 좋은 선택이 아니다 n How to get the Y component from CMSampleBuffer resulted from the AVCaptureSession?

그래서 CMSampleBufferRefUIImage으로 변환하고 YUV 색 공간으로 작업하는 방법이 있습니까?

+0

이에 대한 모든 솔루션으로 설정되어있는 경우 UIImageCMSampleBufferRef를 변환하는 코드를 공유 그래서? –

+0

@AdarshVC 더 눈에 잘 띄게하기 위해 질문에 투표하지 않으시겠습니까? – onmyway133

+0

이 질문을 확인하십시오 http://stackoverflow.com/questions/3305862/uiimage-created-from-cmsamplebufferref-not-displayed-in-uiimageview –

답변

7

많은 연구를 한 후 사과 문서 및 위키피디아를 읽었습니다. 나는 대답을 알아 냈고 완벽하게 작동합니다. 미래의 독자 임 비디오 픽셀 형식이 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange

// Create a UIImage from sample buffer data 
// Works only if pixel format is kCVPixelFormatType_420YpCbCr8BiPlanarFullRange 
-(UIImage *) imageFromSamplePlanerPixelBuffer:(CMSampleBufferRef) sampleBuffer{ 

    @autoreleasepool { 
     // Get a CMSampleBuffer's Core Video image buffer for the media data 
     CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
     // Lock the base address of the pixel buffer 
     CVPixelBufferLockBaseAddress(imageBuffer, 0); 

     // Get the number of bytes per row for the plane pixel buffer 
     void *baseAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0); 

     // Get the number of bytes per row for the plane pixel buffer 
     size_t bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,0); 
     // Get the pixel buffer width and height 
     size_t width = CVPixelBufferGetWidth(imageBuffer); 
     size_t height = CVPixelBufferGetHeight(imageBuffer); 

     // Create a device-dependent gray color space 
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 

     // Create a bitmap graphics context with the sample buffer data 
     CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, 
                bytesPerRow, colorSpace, kCGImageAlphaNone); 
     // Create a Quartz image from the pixel data in the bitmap graphics context 
     CGImageRef quartzImage = CGBitmapContextCreateImage(context); 
     // Unlock the pixel buffer 
     CVPixelBufferUnlockBaseAddress(imageBuffer,0); 

     // Free up the context and color space 
     CGContextRelease(context); 
     CGColorSpaceRelease(colorSpace); 

     // Create an image object from the Quartz image 
     UIImage *image = [UIImage imageWithCGImage:quartzImage]; 

     // Release the Quartz image 
     CGImageRelease(quartzImage); 

     return (image); 
    } 
} 
+0

아웃풋 이미지는 그레이 스케일로 표시됩니다. – Bluewings

+0

다른 색상은 어떻습니까? – JULIIncognito

+0

@JULIIncognito rgb 에의 색 공간을 chnage – Bluewings