AVCaptureSession预设照片和AVCaptureVideoPreviewLayer尺寸

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层在屏幕上?更准确地说,我需要高度,因为AVLayerVideoGravityResizeAspect使AVCaptureVideoPreviewLayer适合preview.size
我试图获得适合的AVCaptureVideoPreviewLayer大小。
非常感谢您的帮助。
4个回答

8

经过对AVCaptureSessionPresetPhoto的研究,AVCaptureVideoPreviewLayer尊重iPhone相机的3/4比例。因此,通过简单的计算可以轻松得到正确的高度。
例如,如果宽度为320,则适当的高度为:
320*4/3=426.6


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

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

例如:

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));
    }
}

我试图获取AVCaptureVideoPreviewLayer的大小。由于我只设置了stillImageoutput输出,因此outputSettings仅提供:AVVideoCodecKey = jpeg; - gsempe
我更新了我的回答。也许你可以从预览图像中获取尺寸。 - Thomas Zoechling
经过使用AVCaptureSessionPresetPhoto进行一些研究,发现AVCaptureVideoPreviewLayer尊重iPhone相机的3/4比例。因此,通过简单的计算很容易得到正确的高度。不过还是感谢您的帮助。 - gsempe

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()函数,以便将一个居中的图层显示在我的屏幕上。
谢谢, Julian

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