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

发布于 2024-12-21 06:36:21 字数 1036 浏览 2 评论 0原文

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

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

I'm trying to grab video with an AVCaptureSession, process the video in the callback (eventually), then render the results into my GLKView. The code below works but the image in my GLKView is rotated 90 degrees and shrunk by 50%.

The glContext is created with [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

My coreImageContext is created with [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];
    });
}

Inserting code to perform and affine transform seems inefficient. Am I missing a setup call or parameter to prevent the rotation and scaling?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不羁少年 2024-12-28 06:36:21

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

The video preview view for AVFoundation does rotations on the images using the graphics hardware. The native capture orientation for the back facing camera is landscape left. It is landscape right for the front facing camera. When you record a video AVFoundation will place a transform in the header of the MOV/MP4 to indicate to a player the correct orientation of the video. If you just pull the images out of the MOV/MP4 they will be in their native capture orientation. Take a look at the two example programs linked in the top post for this SO Post

走过海棠暮 2024-12-28 06:36:21

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

drawImage:inRect:fromRect: will scale the image to fit the destination rectangle. You just need to appropriately scale self.view.bounds:

CGFloat screenScale = [[UIScreen mainScreen] scale];
[self.coreImageContext drawImage:image inRect:CGRectApplyAffineTransform(self.view.bounds, CGAffineTransformMakeScale(screenScale, screenScale)) fromRect:[image extent]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文