从CVPixelBufferRef创建的CIImage在GLKView中绘制旋转和缩小

时间:2011-12-16 00:31:37

标签: ios5 avfoundation core-image

我正在尝试使用AVCaptureSession获取视频,在回调中处理视频(最终),然后将结果渲染到我的GLKView中。下面的代码有效,但我的GLKView中的图像旋转了90度并缩小了50%。

glContext 是使用[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]创建的;

我的 coreImageContext 是使用[CIContext contextWithEAGLContext:glContext]创建的;

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
       fromConnection:(AVCaptureConnection *)connection
{
    // process the image
    CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
    CIImage *image = [CIImage imageWithCVPixelBuffer:pixelBuffer];

    // display it (using main thread)
    dispatch_async( dispatch_get_main_queue(), ^{
        // running synchronously on the main thread now
        [self.coreImageContext drawImage:image inRect:self.view.bounds fromRect:[image extent]];
        [self.glContext presentRenderbuffer:GL_RENDERBUFFER];
    });
}

插入代码来执行和仿射变换似乎效率低下。我是否缺少设置调用或参数来阻止旋转和缩放?

2 个答案:

答案 0 :(得分:3)

AVFoundation的视频预览视图使用图形硬件对图像进行旋转。背面摄像头的原生捕捉方向是横向左侧。它适用于前置摄像头。当您录制视频时,AVFoundation会在MOV / MP4的标题中放置一个变换,以向玩家指示视频的正确方向。如果您只是将图像拉出MOV / MP4,它们将处于其原生捕获方向。请查看此SO Post

顶部帖子中链接的两个示例程序

答案 1 :(得分:0)

drawImage:inRect:fromRect:将缩放图像以适合目标矩形。你只需要适当地扩展self.view.bounds:

CGFloat screenScale = [[UIScreen mainScreen] scale];
[self.coreImageContext drawImage:image inRect:CGRectApplyAffineTransform(self.view.bounds, CGAffineTransformMakeScale(screenScale, screenScale)) fromRect:[image extent]];