Reputation: 31
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?
Upvotes: 3
Views: 3401
Reputation: 143
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]];
Upvotes: 0
Reputation: 3596
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
Upvotes: 4