从AVCaptureSessionDataOutput与AVCaptureSessionPresetPhoto iOS iOS CVImageBuffer扭曲
在很高的层面上,我创build了一个应用程序,让用户指向他或她的iPhone摄像头,并看到视觉效果处理过的video帧。 此外,用户可以点击一个button,将当前预览的冻结帧作为保存在其iPhone库中的高分辨率照片。
要做到这一点,应用程序遵循这个过程:
1)创build一个AVCaptureSession
captureSession = [[AVCaptureSession alloc] init]; [captureSession setSessionPreset:AVCaptureSessionPreset640x480];
2)使用背面照相机连接AVCaptureDeviceInput。
videoInput = [[[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error] autorelease]; [captureSession addInput:videoInput];
3)将AVCaptureStillImageOutput连接到会话,以便能够以照片分辨率捕捉静帧。
stillOutput = [[AVCaptureStillImageOutput alloc] init]; [stillOutput setOutputSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]]; [captureSession addOutput:stillOutput];
4)将AVCaptureVideoDataOutput连接到会话,以便能够以较低的分辨率捕获单独的video帧(CVImageBuffers)
videoOutput = [[AVCaptureVideoDataOutput alloc] init]; [videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]]; [videoOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()]; [captureSession addOutput:videoOutput];
5)当video帧被捕获时,委托的方法被称为每个新的帧作为CVImageBuffer:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); [self.delegate processNewCameraFrame:pixelBuffer]; }
6)然后代表处理/绘制它们:
- (void)processNewCameraFrame:(CVImageBufferRef)cameraFrame { CVPixelBufferLockBaseAddress(cameraFrame, 0); int bufferHeight = CVPixelBufferGetHeight(cameraFrame); int bufferWidth = CVPixelBufferGetWidth(cameraFrame); glClear(GL_COLOR_BUFFER_BIT); glGenTextures(1, &videoFrameTexture_); glBindTexture(GL_TEXTURE_2D, videoFrameTexture_); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(cameraFrame)); glBindBuffer(GL_ARRAY_BUFFER, [self vertexBuffer]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, [self indexBuffer]); glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0)); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); [[self context] presentRenderbuffer:GL_RENDERBUFFER]; glDeleteTextures(1, &videoFrameTexture_); CVPixelBufferUnlockBaseAddress(cameraFrame, 0); }
这一切工作,并导致正确的结果。 我可以看到通过OpenGL处理的640×480的video预览。 它看起来像这样:
但是,如果我从这个会话中捕捉静止图像,其分辨率也将是640×480。 我希望它是高分辨率,所以在第一步我改变预置线:
[captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
这正确地捕获了iPhone4(2592×1936)的最高分辨率的静止图像。
但是,video预览(由代表在步骤5和6中收到)现在看起来像这样:
我已经确认每个其他预设(高,中,低,640×480和1280×720)按预期预览。 但是,照片预设似乎以不同的格式发送缓冲区数据。
我也确认了,在照片预设中发送到缓冲区的数据实际上是有效的图像数据,通过取出缓冲区并创build一个UIImage而不是将其发送到OpenGL:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(CVPixelBufferGetBaseAddress(cameraFrame), bufferWidth, bufferHeight, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); CGImageRef cgImage = CGBitmapContextCreateImage(context); UIImage *anImage = [UIImage imageWithCGImage:cgImage];
这显示了一个未失真的video帧。
我做了一堆search,似乎无法修复它。 我的直觉是这是一个数据格式问题。 也就是说,我相信缓冲区设置是正确的,但是这种行不能理解的格式:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(cameraFrame));
我的直觉是,将外部格式从GL_BGRA更改为其他内容将有所帮助,但不是…通过各种方式,它看起来像缓冲区实际上在GL_BGRA。
有人知道这里发生了什么? 或者你有什么提示,我可能会去debugging为什么会发生这种情况? (奇怪的是,这是发生在iphone4上,但不是在iPhone 3GS上…都运行ios4.3)
这是一个愚蠢的。
正如Lio Ben-Kereth指出的那样,从debugging器中可以看到填充是48
(gdb) po pixelBuffer <CVPixelBuffer 0x2934d0 width=852 height=640 bytesPerRow=3456 pixelFormat=BGRA # => 3456 - 852 * 4 = 48
OpenGL可以弥补这一点,但OpenGL ES 不能 (更多信息在这里OpenGL的SubTexturing )
所以这里是我在OpenGL ES中做的事情:
(CVImageBufferRef)pixelBuffer // pixelBuffer containing the raw image data is passed in /* ... */ glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, videoFrameTexture_); int frameWidth = CVPixelBufferGetWidth(pixelBuffer); int frameHeight = CVPixelBufferGetHeight(pixelBuffer); size_t bytesPerRow, extraBytes; bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer); extraBytes = bytesPerRow - frameWidth*4; GLubyte *pixelBufferAddr = CVPixelBufferGetBaseAddress(pixelBuffer); if ( [[captureSession sessionPreset] isEqualToString:@"AVCaptureSessionPresetPhoto"] ) { glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, frameWidth, frameHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL ); for( int h = 0; h < frameHeight; h++ ) { GLubyte *row = pixelBufferAddr + h * (frameWidth * 4 + extraBytes); glTexSubImage2D( GL_TEXTURE_2D, 0, 0, h, frameWidth, 1, GL_BGRA, GL_UNSIGNED_BYTE, row ); } } else { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, frameWidth, frameHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixelBufferAddr); }
之前,我使用AVCaptureSessionPresetMedium
并获得30fps。 在AVCaptureSessionPresetPhoto
我在iPhone 4上获得16fps。子纹理的循环似乎不影响帧速率。
我在iOS 5上使用iPhone 4。
就这样画出来。
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer); int frameHeight = CVPixelBufferGetHeight(pixelBuffer); GLubyte *pixelBufferAddr = CVPixelBufferGetBaseAddress(pixelBuffer); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)bytesPerRow / 4, (GLsizei)frameHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixelBufferAddr);
好点的垫子。 但事实上填充量较大,这是:
bytesPerRow = 4 * bufferWidth + 48;
它在iphone 4的背面照相机效果很好,解决了sotangochips报道的问题。
Dex,谢谢你的出色答案。 为了使你的代码更通用,我将replace:
if ( [[captureSession sessionPreset] isEqualToString:@"AVCaptureSessionPresetPhoto"] )
同
if ( extraBytes > 0 )
我想我find了你的答案,我很抱歉,因为这不是个好消息。
你可以查看这个链接: http : //developer.apple.com/library/mac/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html
configuration会话
符号:AVCaptureSessionPresetPhoto
分辨率:照片。
评论:完整的照片分辨率。 video输出不支持此function。
sessionPresetPhoto
是捕获最高质量照片的设置。 当我们使用带有预设照片的AVCaptureStillImageOutput
时,从videostream中捕获的帧始终是iPad或iPhone屏幕的分辨率。 iPad Pro 12.9英寸的分辨率为2732×2048。 这意味着我从videostream中捕获的帧是2732 * 2048,但总是失真和移位。 我尝试了上面提到的解决scheme,但它并没有解决我的问题。 最后,我意识到,框架的宽度应该总是可以被8整除,而不是2732。 2732/8 = 341.5。 所以我所做的就是计算宽度和模数。如果模数不等于零,那么我把它加到宽度上。 在这种情况下,2732%8 = 4,然后我得到2732 + 4 = 2736.所以我会设置CVPixelBufferCreate
这个帧的宽度,以初始化我的pixelBuffer( CVPixelBufferRef
)。
你得到的图像缓冲区似乎在最后包含一些填充。 例如
bytesPerRow = 4 * bufferWidth + 12;
这通常是这样做的,因此每个像素行都以16字节的偏移量开始。