【问题标题】:CIImage created from CVPixelBufferRef draws rotated and shrunk in GLKView从 CVPixelBufferRef 创建的 CIImage 在 GLKView 中绘制旋转和缩小
【发布时间】:2012-01-21 15:26:40
【问题描述】:

我正在尝试使用 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];
    });
}

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

【问题讨论】:

  • 这里有同样的问题。我使用了与您的情况几乎相同的代码。我最终放弃并使用仿射变换来校正旋转并调整图像大小以填满整个屏幕。

标签: ios5 avfoundation core-image


【解决方案1】:

AVFoundation 的视频预览视图使用图形硬件对图像进行旋转。背面摄像头的原生拍摄方向是横向左侧。它是前置摄像头的风景。当您录制视频时,AVFoundation 将在 MOV/MP4 的标题中放置一个转换,以向播放器指示视频的正确方向。如果您只是将图像从 MOV/MP4 中拉出,它们将处于其原始捕获方向。看看这个SO Post顶部帖子中链接的两个示例程序

【讨论】:

    【解决方案2】:

    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]];
    

    【讨论】:

      猜你喜欢
      • 2012-04-16
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多