我正在写一个从相机捕捉实时视频并实时处理它的iphone(IOS 4)程序。如何从相机中抓取YUV格式的视频,显示并处理它
我更喜欢以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搜索此代码 – 2011-05-06 07:34:29