8

我正在使用AVCaptureVideoDataOutput并希望转换CMSampleBufferRefUIImage. 很多答案都是一样的,比如这个从 CMSampleBufferRef 创建的 UIImage 没有显示在 UIImageView 中?具有多个预览的 AVCaptureSession

如果我将 VideoDataOutput 颜色空间设置为 BGRA,它工作正常(归功于此答案CGBitmapContextCreateImage 错误

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

如果没有上述 videoSettings,我将收到以下错误

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

使用 BGRA 不是一个好的选择,因为从 YUV(默认 AVCaptureSession 颜色空间)到 BGRA 存在转换开销,正如 Brad 和 Codo 在How to get the Y component from CMSampleBuffer from the AVCaptureSession 中所述?

那么有没有办法转换CMSampleBufferRefUIImage使用 YUV 颜色空间?

4

2 回答 2

9

经过大量研究并阅读了苹果文档和维基百科。我想出了答案,它对我来说非常有效。因此,对于未来的读者,我分享了将视频像素类型设置为时转换CMSampleBufferRef为的代码UIImagekCVPixelFormatType_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);
    }
}
于 2015-05-08T09:24:31.783 回答
-1

// 这个对我有用。

var image: CGImage?
VTCreateCGImageFromCVPixelBuffer(pixelBuffer, options: nil, imageOut: &image)
DispatchQueue.main.async {
    self.imageView.image = UIImage(cgImage: image!)
}
于 2020-10-21T06:39:40.583 回答