AVCaptureSession分辨率不会随AVCaptureSessionPreset更改
问题描述:
我想更改在带有AV Foundation的OS X上使用相机拍摄的图片的分辨率。AVCaptureSession分辨率不会随AVCaptureSessionPreset更改
但即使我改变我的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添加到捕获会话后立即尝试此代码。
//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];
有可能是有其他方法来解决这个问题,但这是固定的我。