【问题标题】:Realtime capture using AVFoundation rotated 90º in AVCaptureVideoPreviewLayer在 AVCaptureVideoPreviewLayer 中使用旋转 90º 的 AVFoundation 进行实时捕捉
【发布时间】:2015-04-21 10:07:04
【问题描述】:

我正在开发一个应用程序,该应用程序允许用户实时录制视频,同时一些 HUD 信息显示在预览层中。

我已经解决了很多问题,但我遇到的一个问题是,当 iPad 处于横向模式时,视频本身会向右旋转 90 度。当主页按钮在左侧时,视频将逆时针旋转 90 度,而当主页按钮在右侧时,视频会顺时针旋转 90 度。无论我使用哪种横向模式,视频都应该是直立的。此 [SO 消息] 似乎与我正在处理的内容非常接近,但该解决方案包含弃用。 1

在我的主控制器中:

[self setCaptureManager:[[[CaptureSessionManager alloc] init] autorelease]];

[[self captureManager] addVideoInput];

[[self captureManager] addVideoPreviewLayer];
CGRect layerRect = [[[self view] layer] bounds];
[[[self captureManager] previewLayer] setBounds:layerRect];
[[[self captureManager] previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),
        CGRectGetMidY(layerRect))];
[[[self view] layer] addSublayer:[[self captureManager] previewLayer]];

CaptureSessionManager.h

#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>


@interface CaptureSessionManager : NSObject {

}

@property(retain) AVCaptureVideoPreviewLayer *previewLayer;
@property(retain) AVCaptureSession *captureSession;

- (void)addVideoPreviewLayer;

- (void)addVideoInput;

@end

CaptureSessionManager.m

#import "CaptureSessionManager.h"


@implementation CaptureSessionManager

@synthesize captureSession;
@synthesize previewLayer;

#pragma mark Capture Session Configuration

- (id)init {
    if ((self = [super init])) {
        [self setCaptureSession:[[AVCaptureSession alloc] init]];
        if ([captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]) {
            captureSession.sessionPreset = AVCaptureSessionPresetHigh;
        }
    }
    return self;
}

- (void)addVideoPreviewLayer {
    [self setPreviewLayer:[[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] autorelease]];
    [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];

}

- (void)addVideoInput {
    AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (videoDevice) {
        NSError *error;
        AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
        if (!error) {
            if ([[self captureSession] canAddInput:videoIn])
                [[self captureSession] addInput:videoIn];
            else
                NSLog(@"Couldn't add video input");
        }
        else
            NSLog(@"Couldn't create video input");
    }
    else
        NSLog(@"Couldn't create video capture device");
}

- (void)dealloc {

    [[self captureSession] stopRunning];

    [previewLayer release], previewLayer = nil;
    [captureSession release], captureSession = nil;

    [super dealloc];
}

@end

【问题讨论】:

标签: ios cocoa-touch video rotation avfoundation


【解决方案1】:

如果您提到的那个问题的答案不起作用,那么您可以通过其他方式实现。使用CATransform3DMakeRotation根据设备的方向旋转预览层。

例如,对于UIDeviceOrientationLandscapeLeft,您可以像这样旋转预览层:

preview.transform = CATransform3DMakeRotation(-M_PI/2, 0, 0, 1);

【讨论】:

    【解决方案2】:

    发现需要设置预览层连接的videoOrientation属性。

    这是对我的 addVideoPreviewLayer 方法的更改...

    - (void)addVideoPreviewLayer {
        [self setPreviewLayer:[[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] autorelease]];
        [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
        [[previewLayer connection] setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-23
      • 1970-01-01
      • 2021-11-23
      • 2013-09-13
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      相关资源
      最近更新 更多