AVCaptureSession 分辨率不会随 AVCaptureSessionPreset 改变

AVCaptureSession resolution doesn't change with AVCaptureSessionPreset

我想更改我在 OS X with AV Foundation 上用相机拍摄的照片的分辨率。

但即使我更改了 AVCaptureSession 的分辨率,输出图片的大小也不会改变。我总是有一张 1280x720 的图片。

我想要较低的分辨率,因为我在实时过程中使用这些图片并且我希望程序更快。

这是我的代码示例:

 session = [[AVCaptureSession alloc] init];

if([session canSetSessionPreset:AVCaptureSessionPreset640x360]) {
    [session setSessionPreset:AVCaptureSessionPreset640x360];
}

AVCaptureDeviceInput *device_input = [[AVCaptureDeviceInput alloc] initWithDevice:
                                       [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo][0] error:nil];

if([session canAddInput:device_input])
    [session addInput:device_input];

still_image = [[AVCaptureStillImageOutput alloc] init];

NSDictionary *output_settings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
[still_image setOutputSettings : output_settings];

[session addOutput:still_image];

我应该在我的代码中更改什么?

我也 运行 解决了这个问题,并找到了一个似乎有效的解决方案。由于某些原因,在 OS X 上,StillImageOutput 破坏了捕获会话预设。

我所做的是直接更改AVCaptureDevice 的活动格式。在将 StillImageOutput 添加到 Capture Session 后立即尝试此代码。

//Get a list of supported formats for the device
NSArray *supportedFormats = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo][0] formats];

//Find the format closest to what you are looking for
//  this is just one way of finding it
NSInteger desiredWidth = 640;
AVCaptureDeviceFormat *bestFormat;
for (AVCaptureDeviceFormat *format in supportedFormats) {
    CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions((CMVideoFormatDescriptionRef)[format formatDescription]);
    if (dimensions.width <= desiredWidth) {
        bestFormat = format;
    }
}

[[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo][0] lockForConfiguration:nil];
[[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo][0] setActiveFormat:bestFormat]; 
[[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo][0] unlockForConfiguration];

可能还有其他方法可以解决这个问题,但我已经解决了这个问题。

所以我也遇到了这个问题,但是使用原始 AVCaptureVideoDataOutput 而不是 JPG。

问题是会话预设 Low/Medium/High 实际上会以某些方式影响捕获设备,例如帧率,但它不会改变硬件捕获分辨率 - 它始终以 1280x720 分辨率捕获。我认为,这个想法是,如果会话预设为中等,则普通的 Quicktime 输出设备会解决这个问题并添加一个缩放步骤到 640x480(例如)。

但是当使用原始输出时,他们不会关心预设的所需尺寸。

与 Apple 的 videoSettings 文档相反,解决方案是将请求的尺寸添加到 videoSettings:

        NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                    [NSNumber numberWithDouble:640], (id)kCVPixelBufferWidthKey,
                    [NSNumber numberWithDouble:480], (id)kCVPixelBufferHeightKey,
                    [NSNumber numberWithInt:kCMPixelFormat_422YpCbCr8_yuvs], (id)kCVPixelBufferPixelFormatTypeKey,
                    nil];
    [captureoutput setVideoSettings:outputSettings];

我说的与 Apple 文档相反,因为文档说 FormatTypeKey 是此处唯一允许的键。但是 bufferheight/width 键确实有效并且是必需的。