1

我正在努力解决的缩放问题仍然存在。我尝试按照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");
}
4

2 回答 2

3

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

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];
于 2014-10-15T15:26:37.217 回答
0

现在让它使用以下代码:

dispatch_async(sessionQueue, ^{
    [self setBackgroundRecordingID:UIBackgroundTaskInvalid];

    NSError *error = nil;

    AVCaptureDevice *videoDevice = [iKKAvCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack];
    AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];

    if (error)
    {
        NSLog(@"%@", error);
    }

    if ([session canAddInput:videoDeviceInput])
    {
        [session addInput:videoDeviceInput];
        [self setVideoDeviceInput:videoDeviceInput];

        dispatch_async(dispatch_get_main_queue(), ^{
            // Why are we dispatching this to the main queue?
            // Because AVCaptureVideoPreviewLayer is the backing layer for AVCamPreviewView and UIView can only be manipulated on main thread.
            // Note: As an exception to the above rule, it is not necessary to serialize video orientation changes on the AVCaptureVideoPreviewLayer’s connection with other session manipulation.

            [[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] setVideoOrientation:(AVCaptureVideoOrientation)[self interfaceOrientation]];

            // Apply initial VideoZoomFactor to the device
            NSNumber *DefaultZoomFactor = [[NSNumber alloc] initWithFloat:3.0];
            NSError *error = nil;
            if ([[[self videoDeviceInput] device] lockForConfiguration:&error])
            {
                [[[self videoDeviceInput] device] setVideoZoomFactor:[DefaultZoomFactor floatValue]];
                self.zoomFactorLabel.text = [NSString stringWithFormat:@"%.1f", [DefaultZoomFactor floatValue]];
                [[[self videoDeviceInput] device] unlockForConfiguration];
            }
            else
            {
                NSLog(@"%@", error);
            }                
        });
    }
});
于 2014-10-15T17:16:26.847 回答