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

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

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


同样的问题。我使用了几乎与你的情况相同的代码。最终我放弃了,并使用仿射变换来纠正旋转并将图像调整大小以填充整个屏幕。 - Di Wu
2个回答

4
AVFoundation 的视频预览视图通过使用图形硬件对图像进行旋转。后置摄像头的本机捕获方向为横向左侧,前置摄像头则为横向右侧。录制视频时,AVFoundation 将在 MOV/MP4 的头部放置一个变换来指示播放器视频的正确方向。如果您只是从 MOV/MP4 中提取图像,则它们将保持其原生捕获方向。请查看此SO帖子中链接的两个示例程序。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接