【问题标题】:Create CVPixelBuffer with pixels data, but the final image is distorted用像素数据创建CVPixelBuffer,但最终图像失真
【发布时间】:2016-09-02 05:32:54
【问题描述】:

我通过OpenGLES方法(glReadPixels)或其他方式获取像素,然后创建CVPixelBuffer(带或不带CGImage)进行视频录制,但最终图片失真。当我在 iPhone 5c、5s 和 6 上测试时,这发生在 iPhone6 上。

看起来像:

代码如下:

CGSize viewSize=self.glView.bounds.size;
NSInteger myDataLength = viewSize.width * viewSize.height * 4;

// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, viewSize.width, viewSize.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y < viewSize.height; y++)
{
    for(int x = 0; x < viewSize.width* 4; x++)
    {
        buffer2[(int)((viewSize.height-1 - y) * viewSize.width * 4 + x)] = buffer[(int)(y * 4 * viewSize.width + x)];
    }
}

free(buffer);

// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * viewSize.width;
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

// make the cgimage
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGImageRef imageRef = CGImageCreate(viewSize.width , viewSize.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

//UIImage *photo = [UIImage imageWithCGImage:imageRef];

int width = CGImageGetWidth(imageRef);
int height = CGImageGetHeight(imageRef);

CVPixelBufferRef pixelBuffer = NULL;
CVReturn status = CVPixelBufferPoolCreatePixelBuffer(NULL, _recorder.pixelBufferAdaptor.pixelBufferPool, &pixelBuffer);

NSAssert((status == kCVReturnSuccess && pixelBuffer != NULL), @"create pixel buffer failed.");

CVPixelBufferLockBaseAddress(pixelBuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pixelBuffer);
NSParameterAssert(pxdata != NULL);//CGContextRef
CGContextRef context = CGBitmapContextCreate(pxdata,
                                             width,
                                             height,
                                             CGImageGetBitsPerComponent(imageRef),
                                             CGImageGetBytesPerRow(imageRef),
                                             colorSpaceRef,
                                             kCGImageAlphaPremultipliedLast);
NSParameterAssert(context);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);

CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);

CGColorSpaceRelease(colorSpaceRef);
CGContextRelease(context);
CGImageRelease(imageRef);

free(buffer2);

//CIImage *image = [CIImage imageWithCVPixelBuffer:pixelBuffer];

// ...

CVPixelBufferRelease(pixelBuffer);

【问题讨论】:

  • 也遇到这个问题... :(
  • 我能够通过确保每一帧的尺寸完全相同来解决这个问题。结果发现有些帧的大小略有不同,导致失真。

标签: ios image opengl-es avfoundation cvpixelbuffer


【解决方案1】:

注意 - 此答案涉及图像的整体问题,而不是特定代码。

这类问题通常是“步幅”,与用于保存图像的内存布局有关,其中每行像素没有紧密地打包在一起

例如,源图像可能是 240 像素宽。 CMPixelBuffer 可以为每行分配 320 个像素,其中前 240 个像素保存图像,额外的 80 个像素用于填充。 在这种情况下,宽度为 240 像素,步幅为 320 像素。

步幅通常意味着您必须循环复制每一行像素

【讨论】:

  • 还有其他提示吗?你能解释一下“stride”以及它与 CGContext 代码的关系吗?
【解决方案2】:

在代码的任何地方都使用这个尺寸

int width_16 = (int)yourImage.size.width - (int)yourImage.size.width%16; 
int height_ = (int)(yourImage.size.height/yourImage.size.width * width_16) ;
CGSize video_size_ = CGSizeMake(width_16, height_);

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,我认为解决方案如下: 尝试在 CGBitmapContextCreate 调用中将 CGImageGetBytesPerRow(imageRef) 更改为 CVPixelBufferGetBytesPerRow(pxbuffer)。原因是您的上下文是由您创建的像素缓冲区的原始数据支持的,而不是您正在绘制的 CGImage。 CVPixelBuffer 的每行字节数可能大于每像素字节数 * 像素缓冲区宽度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-23
      • 2020-07-22
      • 2014-10-24
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多