2010-11-17 26 views
9

我正在写一个从相机捕捉实时视频并实时处理它的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); 
} 
+0

外观极好的例子!我已通过所有apple.com搜索此代码 – 2011-05-06 07:34:29

回答

6

回答我自己的问题。 这个问题解决了我(这是抢YUV输出,显示并处理它),尽管它不完全回答这个问题:

要抓住从摄像机YUV输出:

AVCaptureVideoDataOutput *videoOut = [[AVCaptureVideoDataOutput alloc] init]; 
[videoOut setAlwaysDiscardsLateVideoFrames:YES]; 
[videoOut setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]]; 

要按原样显示,请使用AVCaptureVideoPreviewLayer,它不需要太多代码。 (例如,您可以在WWDC样本包中看到FindMyiCon样本)。

为了处理YUV Y通道(双平面在这种情况下,所以这一切都在一个单一的块,你也可以使用memcpy而不是循环的):

- (void)processPixelBuffer: (CVImageBufferRef)pixelBuffer { 

    CVPixelBufferLockBaseAddress(pixelBuffer, 0); 

    int bufferHeight = CVPixelBufferGetHeight(pixelBuffer); 
    int bufferWidth = CVPixelBufferGetWidth(pixelBuffer); 

    // allocate space for ychannel, reallocating as needed. 
    if (bufferWidth != y_channel.width || bufferHeight != y_channel.height) 
    { 
     if (y_channel.data) free(y_channel.data); 
     y_channel.width = bufferWidth; 
     y_channel.height = bufferHeight; 
     y_channel.data = malloc(y_channel.width * y_channel.height);   
    } 

    uint8_t *yc = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0); 
    int total = bufferWidth * bufferHeight; 
    for(int k=0;k<total;k++) 
    { 
     y_channel.data[k] = yc[k++]; // copy y channel 
    } 

    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0); 
} 
相关问题