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 = CMSampleB开发者_如何学运维ufferGetImageBuffer(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
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.
精彩评论