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