- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
从iOS6开始,Apple通过这个调用给CIImage提供了使用原生YUV的规定
initWithCVPixelBuffer:options:
在核心图像编程指南中,他们提到了这个特性
Take advantage of the support for YUV image in iOS 6.0 and later. Camera pixel buffers are natively YUV but most image processing algorithms expect RBGA data. There is a cost to converting between the two. Core Image supports reading YUB from CVPixelBuffer objects and applying the appropriate color transform.
options = @{ (id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCvCr88iPlanarFullRange) };
但是,我无法正确使用它。我有一个原始的 YUV 数据。所以,这就是我所做的
void *YUV[3] = {data[0], data[1], data[2]};
size_t planeWidth[3] = {width, width/2, width/2};
size_t planeHeight[3] = {height, height/2, height/2};
size_t planeBytesPerRow[3] = {stride, stride/2, stride/2};
CVPixelBufferRef pixelBuffer = NULL;
CVReturn ret = CVPixelBufferCreateWithPlanarBytes(kCFAllocatorDefault,
width,
height,
kCVPixelFormatType_420YpCbCr8PlanarFullRange,
nil,
width*height*1.5,
3,
YUV,
planeWidth,
planeHeight,
planeBytesPerRow,
nil,
nil, nil, &pixelBuffer);
NSDict *opt = @{ (id)kCVPixelBufferPixelFormatTypeKey :
@(kCVPixelFormatType_420YpCbCr8PlanarFullRange) };
CIImage *image = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:opt];
我的图像为零。知道我错过了什么。
编辑: 我在调用前添加了锁定和解锁基址。另外,我转储了 pixelbuffer 的数据,以确保 pixellbuffer 能够正确保存数据。看起来只有 init 调用有问题。 CIImage 对象仍然返回 nil。
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
CIImage *image = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:opt];
CVPixelBufferUnlockBaseAddress(pixelBuffer,0);
最佳答案
控制台中应该有错误消息:initWithCVPixelBuffer failed because the CVPixelBufferRef is not IOSurface backed
。参见 Apple 的 Technical Q&A QA1781了解如何创建 IOSurface 支持的 CVPixelBuffer
。
Calling
CVPixelBufferCreateWithBytes()
orCVPixelBufferCreateWithPlanarBytes()
will result inCVPixelBuffers
that are not IOSurface-backed......To do that, you must specify
kCVPixelBufferIOSurfacePropertiesKey
in thepixelBufferAttributes
dictionary when creating the pixel buffer usingCVPixelBufferCreate()
.
NSDictionary *pixelBufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSDictionary dictionary], (id)kCVPixelBufferIOSurfacePropertiesKey,
nil];
// you may add other keys as appropriate, e.g. kCVPixelBufferPixelFormatTypeKey, kCVPixelBufferWidthKey, kCVPixelBufferHeightKey, etc.
CVPixelBufferRef pixelBuffer;
CVPixelBufferCreate(... (CFDictionaryRef)pixelBufferAttributes, &pixelBuffer);
Alternatively, you can make IOSurface-backed
CVPixelBuffers
usingCVPixelBufferPoolCreatePixelBuffer()
from an existing pixel buffer pool, if thepixelBufferAttributes
dictionary provided toCVPixelBufferPoolCreate()
includeskCVPixelBufferIOSurfacePropertiesKey
.
关于iOS6 : How to use the conversion feature of YUV to RGB from cvPixelBufferref to CIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19851539/
我是一名优秀的程序员,十分优秀!