iphone - 从相机输入复制像素

标签 iphone c opengl-es camera avfoundation

我想从相机中获取图像的像素。 我正在使用 CoreFoundation 和 OpenGL,我可以渲染图像,但我想做一些其他事情(在其他地方/线程),所以我需要复制它们。

这是我尝试过的:(AVCaptureVideoDataOutputSampleBufferDelegate 方法的一部分)

CVImageBufferRef tmpPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress( tmpPixelBuffer, 0 );

//APPROACH A
CFRetain(tmpPixelBuffer);
if(pixelBuffer!= 0) CFRelease(pixelBuffer);
pixelBuffer = tmpPixelBuffer;

//create and bind textureHandle 
if(m_textureHandle==0) m_textureHandle = [self _createVideoTextureUsingWidth:videoDimensions.width Height:videoDimensions.width];
glBindTexture(GL_TEXTURE_2D, m_textureHandle);

unsigned char *linebase = (unsigned char *)CVPixelBufferGetBaseAddress( tmpPixelBuffer );

//APPROACH B
unsigned size = videoDimensions.width*videoDimensions.height*4;//since its BGRA
unsigned char *imageData = malloc(size);
memcpy(linebase, imageData, size);
free(imageData);

//map the texture   
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, videoDimensions.width, videoDimensions.height, GL_BGRA_EXT, GL_UNSIGNED_BYTE, linebase);

CVPixelBufferUnlockBaseAddress( tmpPixelBuffer, 0 );

在方法 A 中,我试图保留孔缓冲区,以便稍后在需要时复制像素,但我总是得到一个 NULL 指针(我不知道为什么)

在方法 B 中,我尝试每次都复制像素,但随后 opengl 渲染器将显示黑色图像。我不知道为什么,我不是在修改原始缓冲区吗?

提前致谢;)

伊格纳西奥

最佳答案

在您的方法 B 中,您在将像素缓冲区复制到其中后立即释放 imageData。如果您在 Debug模式下运行,则内存管理器可能正在清除内存。在任何情况下,在您完全处理完数据之前不要调用 free()。

关于iphone - 从相机输入复制像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4417969/

相关文章:

iphone - 在 ios6 中解析 csv

ios - 在 ios 中使用 Collection View 创建 View

c - 应该使用哪个 4.x 版本的 gcc?

c - DbgPrint 输出与宏到 DbgPrint 的输出不同

iphone - 更改 OpenGL 中的纹理不透明度

android - 增强现实 - 将 GPS 映射到 OpenGL

ios - 如何在 Xcode 9 中禁用字体平滑?

iphone - iOS - QuickLook - 如何在没有 UIScrollView 的情况下在 QuickLook 中打开对象

c - 为 Ruby 1.9.1 构建 dnssd gem 时缺少 htons

java - 在 Android 上的 OpenGL ES 中交错的每个顶点数据的偏移量