知道AVCaptureSession的会话预设的解决方案

Fra*_*y I 14 avfoundation core-video ios

我在iOS中访问相机并使用会话预设,如下所示:

captureSession.sessionPreset = AVCaptureSessionPresetMedium;
Run Code Online (Sandbox Code Playgroud)

很标准的东西.但是,我想提前知道由于这个预设而我将获得的视频分辨率(特别是因为取决于设备它会有所不同).我知道有在线表格可以查看(例如:http://cmgresearch.blogspot.com/2010/10/augmented-reality-on-iphone-with-ios40.html).但是我希望能够以编程方式获得这个,这样我才不仅仅依赖于魔术数字.

所以,这样的事情(理论上):

[captureSession resolutionForPreset:AVCaptureSessionPresetMedium];
Run Code Online (Sandbox Code Playgroud)

可能会返回{宽度:360,高度:480}的CGSize.我还没有找到任何这样的API,到目前为止,我不得不求助于等待获取我的第一个捕获的图像然后查询它(由于我的程序流中的其他原因并不好).

Chr*_*eer 22

我不是AVFoundation专业人士,但我认为要走的路是:

captureSession.sessionPreset = AVCaptureSessionPresetMedium;
AVCaptureInput *input = [captureSession.inputs objectAtIndex:0]; // maybe search the input in array
AVCaptureInputPort *port = [input.ports objectAtIndex:0];
CMFormatDescriptionRef formatDescription = port.formatDescription;
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
Run Code Online (Sandbox Code Playgroud)

我不确定最后一步,我自己也没试过.刚刚在文档中发现并认为它应该有效.

CMVideoDimensions在Xcode中搜索,您将找到RosyWriter示例项目.看看那段代码(我现在没时间做).

  • 上面的代码对我有用 - 在`[capSession commitConfiguration]`之后调用它,它在前后摄像头之间切换时有效.多么愚蠢的信息清道夫狩猎! (2认同)

Jon*_*ark 7

仅供参考,我在此附上Apple的正式回复.


这是Bug ID#13201137的后续行动.

Engineering已根据以下信息确定此问题的行为符合预期:

包含的代码有几个问题:

1)AVCaptureSession没有输入.2)AVCaptureSession没有输出.

没有至少一个输入(使用[AVCaptureSession addInput:]添加到会话中)和兼容输出(使用[AVCaptureSession addOutput:]添加),将没有活动连接,因此,会话实际上不会在输入中运行设备.它不需要 - 没有输出可以提供任何相机数据.

3)JAViewController类假定一旦[AVCaptureSession startRunning]返回,视频端口的-formatDescription属性将不为零.

一旦startRunning返回,无法保证格式描述将使用新的相机格式更新.-startRunning启动摄像机并在摄像机完全启动并运行时返回,但不等待视频帧主动流过捕获管道,这时格式描述将被更新.

你只是查询得太快了.如果你等了几毫秒,它就会在那里.但正确的方法是监听AVCaptureInputPortFormatDescriptionDidChangeNotification.

4)你的JAViewController类在retrieveCameraInfo中创建一个PVCameraInfo对象并询问它一个问题,然后让它超出范围,在那里它被释放和解除分配.

因此,会话没有足够长的时间来运行以满足您的维度请求.你太快停了相机.

我们认为这个问题已经结束 如果您对此问题有任何疑问或疑虑,请直接更新您的报告(http://bugreport.apple.com).

感谢您抽出宝贵时间通知我们此问题.

最好的祝福,

开发人员错误报告团队Apple Worldwide Developer Relations


Joh*_*rug 6

根据 Apple 的说法,没有 API。太臭了,我也遇到了同样的问题。


Cra*_*lot 5

您可以从activeFormat捕获开始之前以编程方式获取分辨率,但不能在添加输入和输出之前获取分辨率:https : //developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureDevice_Class/index.html#//apple_ref/occ / instp / AVCaptureDevice / activeFormat

private func getCaptureResolution() -> CGSize {
    // Define default resolution
    var resolution = CGSize(width: 0, height: 0)

    // Get cur video device
    let curVideoDevice = useBackCamera ? backCameraDevice : frontCameraDevice

    // Set if video portrait orientation
    let portraitOrientation = orientation == .Portrait || orientation == .PortraitUpsideDown

    // Get video dimensions
    if let formatDescription = curVideoDevice?.activeFormat.formatDescription {
        let dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription)
        resolution = CGSize(width: CGFloat(dimensions.width), height: CGFloat(dimensions.height))
        if (portraitOrientation) {
            resolution = CGSize(width: resolution.height, height: resolution.width)
        }
    }

    // Return resolution
    return resolution
}
Run Code Online (Sandbox Code Playgroud)