我正在尝试将 CMSampleBufferRef
中的图像裁剪为特定大小。我正在执行 5 个步骤 - 1. 从 SampleBuffer
获取 PixelBuffer
2. 将 PixelBuffer
转换为 CIImage
3. 裁剪 CIImage
4. 将 CIImage
渲染回 PixelBuffer
5. 将 PixelBuffer
附加到 SampleBuffer
。到目前为止,我在第 4 步时遇到问题 - 将图像渲染回 PixelBuffer
(不能检查超出这一点)没有渲染到缓冲区(我使用相同的 CIImage imageWithCVPixelBuffer
检查它并得到 NULL 作为返回)。非常感谢任何提示或帮助。
CGRect cropRect = CGRectMake(0, 0, 640, 480);
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:(CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer)]; //options: [NSDictionary dictionaryWithObjectsAndKeys:[NSNull null], kCIImageColorSpace, nil]];
ciImage = [ciImage imageByCroppingToRect:cropRect];
CVPixelBufferRef pixelBuffer;
CVPixelBufferCreate(kCFAllocatorSystemDefault, 640, 480, kCVPixelFormatType_32BGRA, NULL, &pixelBuffer);
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
CIContext * ciContext = [CIContext contextWithOptions: nil];
[ciContext render:ciImage toCVPixelBuffer:pixelBuffer];
CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );
CMSampleTimingInfo sampleTime = {
.duration = CMSampleBufferGetDuration(sampleBuffer),
.presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer),
.decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer)
};
CMVideoFormatDescriptionRef videoInfo = NULL;
CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, &videoInfo);
CMSampleBufferRef oBuf;
CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, true, NULL, NULL, videoInfo, &sampleTime, &oBuf);
所以我找到了我遇到问题的原因。显然,我必须在初始化像素缓冲区时通过创建字典并将其传递给初始化程序来添加 kCVPixelBufferIOSurfacePropertiesKey 键。如果有人偶然发现这个问题,这里有一个代码:
CFDictionaryRef empty; // empty value for attr value.
CFMutableDictionaryRef attrs;
empty = CFDictionaryCreate(kCFAllocatorDefault, // our empty IOSurface properties dictionary
NULL,
NULL,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
attrs = CFDictionaryCreateMutable(kCFAllocatorDefault,
1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(attrs,
kCVPixelBufferIOSurfacePropertiesKey,
empty);
然后你只需要将这个字典作为参数传递给 CVPixelBufferCreate
我在这里找到了这个解决方案:http://allmybrain.com/2011/12/08/rendering-to-a-texture-with-ios-5-texture-cache-api/
Tôi là một lập trình viên xuất sắc, rất giỏi!