ios 通过代码调整焦距

Posted hherima

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios 通过代码调整焦距相关的知识,希望对你有一定的参考价值。

        我们在自定义相机时,若要实现镜头变焦,也就是推近或者拉远焦距,ios 7以后的实现:可以通过修改AVCaptureDevice的 方法rampToVideoZoomFactor withRate 来实现镜头变焦,带有动画(本质上是修改AVCaptureDevice的缩放系数videoZoomFactor),代码如下(通过手势缩放):

//最小缩放值
- (CGFloat)minZoomFactor

    CGFloat minZoomFactor = 1.0;
    if (@available(iOS 11.0, *)) 
        minZoomFactor = self.device.minAvailableVideoZoomFactor;
    
    return minZoomFactor;


//最大缩放值
- (CGFloat)maxZoomFactor

    CGFloat maxZoomFactor = self.device.activeFormat.videoMaxZoomFactor;
    if (@available(iOS 11.0, *)) 
        maxZoomFactor = self.device.maxAvailableVideoZoomFactor;
    
    
    if (maxZoomFactor > 6.0) 
        maxZoomFactor = 6.0;
    
    return maxZoomFactor;

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

    if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]])
        self.currentZoomFactor = self.device.videoZoomFactor;
    
    return YES;

//缩放手势
- (void)zoomChangePinchGestureRecognizerClick:(UIPinchGestureRecognizer *)pinchGestureRecognizer

    if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan ||
        pinchGestureRecognizer.state == UIGestureRecognizerStateChanged)
    
        CGFloat currentZoomFactor = self.currentZoomFactor * pinchGestureRecognizer.scale;
        
        [self changeFactor:currentZoomFactor];
    
    else
    
    

-(void) changeFactor:(CGFloat)currentZoomFactor
    if (currentZoomFactor < self.maxZoomFactor &&
        currentZoomFactor > self.minZoomFactor)
        
        NSError *error = nil;
        if ([self.device lockForConfiguration:&error] ) 
//            dispatch_async(dispatch_get_main_queue(), ^
                [self.device rampToVideoZoomFactor:currentZoomFactor withRate:3];//rate越大,动画越慢
//                self.device.videoZoomFactor = currentZoomFactor;//无动画
                [self.device unlockForConfiguration];
//            );

        
        else 
            QRLog( @"Could not lock device for configuration: %@", error );
        
    


UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(zoomChangePinchGestureRecognizerClick:)];
    pinchGesture.delegate = self;
    [self.view addGestureRecognizer:pinchGesture];
    self.currentZoomFactor = 1;

总之:缩放只用到了系统的API             [self.device rampToVideoZoomFactor:currentZoomFactor withRate:3];//rate越大动画越慢

之前版本缩放还可以通过AVCaptureConnection,这里不做介绍了。

参考对象:苏沫离  https://www.jianshu.com/p/3f9b2b0ab8d4

 

以上是关于ios 通过代码调整焦距的主要内容,如果未能解决你的问题,请参考以下文章

工业相机对视觉进行了标定后,在调整焦距后对标定精度有影响吗?

相机光圈是如何区分级数的?

iOS 4.2,寻找一种方法来操纵 iPhone 4 相机的焦距

镜头调焦和对焦的区别

不失真的鱼眼图像错误

在做凸透镜成像的实验时,为啥在光屏上找不到像