从相机输入复制像素

发布于 2024-10-06 20:40:11 字数 1281 浏览 4 评论 0原文

我想从相机获取图像的像素。 我正在使用 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 );

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

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

提前致谢;)

伊格纳西奥

I want to get the pixels of the image from the camera.
I am using CoreFoundation and OpenGL and I can render the image but I want to do some other other things (in other place/thread) so I need to copy them.

This is what I have tried:(part of AVCaptureVideoDataOutputSampleBufferDelegate method)

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 );

In approach A I have tried to retain the hole buffer so I can copy the pixels later when needed, but I always get a NULL pointer(I don't know why)

In approach B I try to copy the pixels everytime but I then opengl renderer will show a black image. I don't know why, I am not modifying the original buffer am I?

Thanks in advance ;)

Ignacio

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲琵琶半遮面シ 2024-10-13 20:40:11

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

In your approach B, you free the imageData immediately after you have copied the pixel buffer into it. If you are running in debug mode it's possible that the memory manager is clearing the memory. In any case, don't call free() until you are completely finished with the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文