5

我初始化一个AVCaptureSession并像这样预设它:

AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init];
if (YES==[newCaptureSession canSetSessionPreset:AVCaptureSessionPresetPhoto]) {
    newCaptureSession.sessionPreset = AVCaptureSessionPresetPhoto;
} else {
    // Error management
}

然后我设置一个AVCaptureVideoPreviewLayer

self.preview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/*426*/)];
CALayer *previewLayer = preview.layer;
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
captureVideoPreviewLayer.frame = previewLayer.frame;
[previewLayer addSublayer:captureVideoPreviewLayer];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspect;

我的问题是:
如何获得在屏幕上CGSize显示所有captureVideoPreviewLayer图层所需的确切信息?更准确地说,我需要高度AVLayerVideoGravityResizeAspectAVCaptureVideoPreviewLayer适应preview.size
我尝试获得合适的 AVCaptureVideoPreviewLayer 大小。

非常感谢您的帮助

4

4 回答 4

8

经过对 AVCaptureSessionPresetPhoto 的一些研究,AVCaptureVideoPreviewLayer 尊重 iPhone 相机的 3/4 比例。因此,通过简单的微积分很容易获得正确的身高。
例如,如果宽度为320,则适当的高度为:
320*4/3= 426.6

于 2013-01-06T20:27:57.343 回答
2

Weischel 的代码对我不起作用。这个想法奏效了,但代码没有。这是确实有效的代码:

    // Get your AVCaptureSession somehow. I'm getting mine out of self.videoCamera, which is a GPUImageVideoCamera
    // Get the appropriate AVCaptureVideoDataOutput out of the capture session. I only have one session, so it's easy.

    AVCaptureVideoDataOutput *output = [[[self.videoCamera captureSession] outputs] lastObject];
    NSDictionary* outputSettings = [output videoSettings];

    // AVVideoWidthKey and AVVideoHeightKey did not work. I had to use these literal keys.
    long width  = [[outputSettings objectForKey:@"Width"]  longValue]; 
    long height = [[outputSettings objectForKey:@"Height"] longValue];

    // video camera output dimensions are always for landscape mode. Transpose if your camera is in portrait mode.
    if (UIInterfaceOrientationIsPortrait([self.videoCamera outputImageOrientation])) {
        long buf = width;
        width = height;
        height = buf;
    }

    CGSize outputSize = CGSizeMake(width, height);
于 2013-09-09T17:21:32.153 回答
1

如果我理解正确,您会尝试获取当前视频会话的宽度和高度。
你可以从你的 .outputSettings 字典中获取它们AVCaptureOutput。(使用AVVideoWidthKey& AVVideoHeightKey)。

例如

NSDictionary* outputSettings = [movieFileOutput outputSettingsForConnection:videoConnection];
CGSize videoSize = NSMakeSize([[outputSettings objectForKey:AVVideoWidthKey] doubleValue], [[outputSettings objectForKey:AVVideoHeightKey] doubleValue]);

更新:
另一个想法是从预览会话的图像缓冲区中获取帧大小。
实现 AVCaptureVideoDataOutputSampleBufferDelegate 方法captureOutput:didOutputSampleBuffer:fromConnection: (不要忘记设置 AVCaptureOutput 的委托)

- (void)captureOutput:(AVCaptureFileOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    if(imageBuffer != NULL)
    {
        CGSize imageSize = CVImageBufferGetDisplaySize(imageBuffer);
        NSLog(@"%@", NSStringFromSize(imageSize));
    }
}
于 2013-01-04T09:34:12.023 回答
1

感谢 gsempe 的回答。从几个小时开始我就遇到了同样的问题:)我用这段代码解决了这个问题,以横向模式将其置于屏幕中心:

CGRect layerRect = [[[self view] layer] bounds];
[PreviewLayer setBounds:CGRectMake(0, 0, 426.6, 320)];
[PreviewLayer setPosition:CGPointMake(CGRectGetMidY(layerRect), CGRectGetMidX(layerRect))];

请注意,我必须反转 CGRectGetMidY() 和 CGRectGetMidX() 函数才能在我的屏幕上放置一个居中的图层。

谢谢,

朱利安

于 2014-03-23T22:03:24.840 回答