English 中文(简体)
• 如何从摄像机中摄取图像,显示并处理。
原标题:How to grab YUV formatted video from the camera, display it and process it

我正在撰写一门电话(IOS 4)节目,从摄像机中摄取实况录像并实时处理。

我更喜欢用KCVPixelFormatType_420YpCbCr8捕获。 BiPlanarFullRange格式便于处理(我需要处理Y频道)。 我如何以这种格式展示数据? 我相信,我需要做些什么,把它改成UIImage,然后把它放在一些图像学吗?

Currently I have code that displays kCVPixelFormatType_32BGRA data, but naturally it does not work with kCVPixelFormatType_420YpCbCr8BiPlanarFullRange.

This is the code I use now for the transformation, any help/sample on how to do the same for kCVPixelFormatType_420YpCbCr8BiPlanarFullRange will be appreciated. (Also criticism of my current method).

// 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);
}
最佳回答

Answering my own question. this solved the problem I had (which was to grab yuv output, display it and process it), although its not exactly the answer to the question:

a. 摄像机的精彩产出:

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

用AVCaptureVideoPreview Layer来显示这一点,它并不要求制定任何法典。 (例如,见WWDC样本包中的FindMyiCon样本)。

为了处理“YUV”和“A”频道(在这种情况下是双轨机,这样它就能够一只chu,你也可以使用薄膜而不是住宿):

- (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 );
}
问题回答

暂无回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签