【问题标题】:AVCaptureSession resolution doesn't change with AVCaptureSessionPresetAVCaptureSession 分辨率不会随 AVCaptureSessionPreset 改变
【发布时间】:2016-09-09 10:19:19
【问题描述】:

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

但即使我更改了 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];

我应该对我的代码进行哪些更改?

【问题讨论】:

    标签: objective-c macos avcapturesession avcapture avcaptureoutput


    【解决方案1】:

    我也遇到了这个问题,并找到了一个似乎可行的解决方案。出于某种原因,在 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];
    

    可能有其他方法可以解决此问题,但这已为我解决。

    【讨论】:

    • 遇到了同样的问题,但这并不能解决我的问题:(无论我以哪种顺序设置预设或 setActiveFormat(设置为
    【解决方案2】:

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

    问题在于会话预设低/中/高实际上会在某些方面影响捕获设备,例如帧速率,但它不会改变硬件捕获分辨率 - 它始终会以 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];
    

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

    【讨论】:

      猜你喜欢
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      相关资源
      最近更新 更多