你好,我使用以下函数将sampleBuffer绑定到opengl纹理,效果很好。
void *imageData;
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
UIImage * image = [self generateUIImageFromSampleBuffer:sampleBuffer];
if(imageData == NULL){
width = CGImageGetWidth(image.CGImage);
height = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
imageData = malloc( height * width * 4 );
imgcontext = CGBitmapContextCreate(imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGColorSpaceRelease( colorSpace );
}
CGContextClearRect( imgcontext, CGRectMake( 0, 0, width, height ) );
CGContextTranslateCTM( imgcontext, 0, height - height );
CGContextDrawImage( imgcontext, CGRectMake( 0, 0, width, height ), image.CGImage );
glBindTexture( GL_TEXTURE_2D, m_textureId );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
}
我的问题是,我试图将imageData复制到双数组中,这样我就可以循环所有像素,在不同的过程中做一些处理。这是我使用的代码,在没有给我我期望的结果。
double data[width * height * 4];
memcpy(data, imageData, width * height * 4);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x += 4)
{
double r = data[ y * width + x];
double g = data[ y * width + x + 1];
double b = data[ y * width + x + 2];
double a = data[ y * width + x + 3];
}
}
这段代码在纹理绑定后被直接调用,但是r, g, b永远不会改变值。你知道我可能做错了什么吗
欢呼
看起来您正在分配imageData
以将像素值存储为32位(4字节)整数,但随后试图将每个条目视为double
(8字节)。这是行不通的。
下面的文章可能会有所帮助:
如何从UIImage (Cocoa Touch)或CGImage (Core Graphics)获得像素数据?