我正在努力使用的变焦仍然存在问题。我尝试按照link1link2的代码使用AVCaptureSession获取图像。

一切工作都非常好,除非我尝试实现相机缩放,否则一切都不会如我所愿。

我尝试在AVCaptureDevice对象上使用videoZoomFactor方法,如下面的代码所示。

您有什么建议,为什么videoZoomFactor没有任何作用?

您如何按预期使用AVCaptureSession为静止图像采集创建缩放?

- (void)addVideoInput {
    AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (videoDevice) {

        // SOMEHOW NOT HAVING AN EFFECT AT ALL - WHY ?????
        if ([videoDevice lockForConfiguration:nil]) {
            float zoomFactor = videoDevice.activeFormat.videoZoomFactorUpscaleThreshold;
            [videoDevice setVideoZoomFactor:zoomFactor];
            // [videoDevice setVideoZoomFactor:3];
            // videoDevice.videoZoomFactor = 3;
            [videoDevice unlockForConfiguration];
        }

        // that all works again nicely...
        NSError *error;
        AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
        if (!error) {
            if ([[self captureSession] canAddInput:videoIn]) {
                [[self captureSession] addInput:videoIn];
            }
            else
                NSLog(@"Couldn't add video input");
        }
        else
            NSLog(@"Couldn't create video input");
    }
    else
        NSLog(@"Couldn't create video capture device");
}

最佳答案

并非所有格式都支持缩放。
如果您打印当前格式

NSLog(@"Current format: %@, max zoom factor: %f", videoDevice.activeFormat, videoDevice.activeFormat.videoMaxZoomFactor);

您可能会得到类似的信息(没有缩放级别)。
Current format: <AVCaptureDeviceFormat: 0x1559b3a0 'vide'/'420v' 1280x 720, { 1- 30 fps}, fov:49.240>, max zoom level: 1.000000

然后,如果您打印所有格式
NSLog(@"All formats: %@", videoDevice.formats);

您会注意到其中有些具有缩放因子,有些没有
All formats: (
    "<AVCaptureDeviceFormat: 0x1556aef0 'vide'/'420v'  192x 144, { 1- 30 fps}, fov:64.000, binned>",
    "<AVCaptureDeviceFormat: 0x1556f4e0 'vide'/'420f'  192x 144, { 1- 30 fps}, fov:64.000, binned>",
    "<AVCaptureDeviceFormat: 0x1556f3e0 'vide'/'420v'  352x 288, { 1- 30 fps}, fov:61.260, binned>",
    "<AVCaptureDeviceFormat: 0x1556f3f0 'vide'/'420f'  352x 288, { 1- 30 fps}, fov:61.260, binned>",
    "<AVCaptureDeviceFormat: 0x1556f360 'vide'/'420v'  480x 360, { 1- 30 fps}, fov:64.000, binned>",
    "<AVCaptureDeviceFormat: 0x1556f370 'vide'/'420f'  480x 360, { 1- 30 fps}, fov:64.000, binned>",
    "<AVCaptureDeviceFormat: 0x1556ae30 'vide'/'420v'  640x 480, { 1- 30 fps}, fov:64.000, binned>",
    "<AVCaptureDeviceFormat: 0x1556ae40 'vide'/'420f'  640x 480, { 1- 30 fps}, fov:64.000, binned>",
    "<AVCaptureDeviceFormat: 0x1556f5c0 'vide'/'420v'  960x 540, { 1- 30 fps}, fov:58.460, binned>",
    "<AVCaptureDeviceFormat: 0x1556f5d0 'vide'/'420f'  960x 540, { 1- 30 fps}, fov:58.460, binned>",
    "<AVCaptureDeviceFormat: 0x1556f3a0 'vide'/'420v' 1280x 720, { 1- 30 fps}, fov:49.240>",
    "<AVCaptureDeviceFormat: 0x1556f3b0 'vide'/'420f' 1280x 720, { 1- 30 fps}, fov:49.240>",
    "<AVCaptureDeviceFormat: 0x1556aa60 'vide'/'420v' 2592x1936, { 1- 15 fps}, fov:64.000, max zoom:121.00 (upscales @1.00)>",
    "<AVCaptureDeviceFormat: 0x1556aa70 'vide'/'420f' 2592x1936, { 1- 15 fps}, fov:64.000, max zoom:121.00 (upscales @1.00)>"

就我而言,最后两个具有最大变焦。在这种情况下,我将我的activeFormat设置为最后一个(最大缩放大于1(1 =无缩放))。您不应该像我的示例(lastObject)那样使用它,首先应该检查哪种缩放比例大于1。
videoDevice.activeFormat = [videoDevice.formats lastObject];

现在,您可以在1.0和videoMaxZoomFactor之间设置任何数字。
[videoDevice setVideoZoomFactor:5.0];

关于ios - videoZoomFactor无法用于AVCaptureSession,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25125077/

10-13 04:34