首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >裁剪CMSampleBufferRef

裁剪CMSampleBufferRef
EN

Stack Overflow用户
提问于 2013-03-29 07:13:48
回答 1查看 4.6K关注 0票数 5

我正在尝试在CMSampleBufferRef中裁剪图像到特定的大小。我正在进行5个步骤- 1.从SampleBuffer获取PixelBuffer 2.将PixelBuffer转换为CIImage 3.裁剪CIImage 4.将CIImage渲染回PixelBuffer 5.将PixelBuffer附加到SampleBuffer。到目前为止,我在第4步中遇到了问题-将图像渲染回PixelBuffer (不能检查超出这一点)没有渲染到缓冲区(我使用相同的CIImage imageWithCVPixelBuffer检查它,并获取NULL作为返回)。将非常感谢任何提示或帮助。

代码语言:javascript
运行
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-30 06:35:33

所以我找到了我有问题的原因。显然,在初始化像素缓冲区时,我必须添加kCVPixelBufferIOSurfacePropertiesKey键,方法是创建字典并将其传递给初始化器。如果有人遇到这个问题,这里有一段代码:

代码语言:javascript
运行
复制
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/

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15693784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档