【发布时间】:2010-11-17 14:09:51
【问题描述】:
我正在编写一个 iphone (IOS 4) 程序,该程序从摄像头捕获实时视频并实时处理。
我更喜欢以 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange 格式进行捕获,以便于处理(我需要处理 Y 通道)。如何以这种格式显示数据?我想我需要以某种方式将其转换为 UIImage,然后将其放入某个 ImageView 中?
目前我有显示 kCVPixelFormatType_32BGRA 数据的代码,但它自然不适用于 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange。
这是我现在用于转换的代码,任何有关如何对 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange 执行相同操作的帮助/示例将不胜感激。 (也批评我目前的方法)。
// Create a UIImage from sample buffer data
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
// 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 pixel buffer
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
// Get the pixel buffer width and height
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Create a bitmap graphics context with the sample buffer data
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
// 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);
}
【问题讨论】:
-
很好的例子!我已经通过所有 apple.com 搜索了此代码
标签: iphone objective-c cocoa-touch uikit ios4