美文网首页
相机调整焦距 0.5x 1x 2.0x 2.5x 3x的获取

相机调整焦距 0.5x 1x 2.0x 2.5x 3x的获取

作者: follow_er | 来源:发表于2022-08-01 14:41 被阅读0次

    获取可以设置的焦距值

            let device: AVCaptureDevice = cameraManager.backFacingCamera()
            var arrTitleAll: [String] = []
            if #available(iOS 13.0, *) {
                let arrFactors = device.virtualDeviceSwitchOverVideoZoomFactors
                if arrFactors.isEmpty == false {
                    arrTitleAll.append("0.5x")
                    for factor in arrFactors {
                        let trueFactor = factor.floatValue * 0.5
                        let strFactor = NSString(format: "%@", NSNumber(value: trueFactor)) as String
                        let title = strFactor as String + "x"
                        arrTitleAll.append(title)
                    }
                } else {
                    arrTitleAll = ["0.5x", "1x"]
                }
                
            } else {
                arrTitleAll = ["0.5x", "1x"]
            }
            arrBtnFocusTitleAll = arrTitleAll
            btnFocalLength.tag = 1
            changeCameraZoom()
    
    

    设置焦距

    func changeCameraZoom() {
            //调整缩放因子
            let floatValue = ((btnFocalLength.currentTitle ?? "") as NSString).floatValue
            var factor = Float(floatValue)
            factor = factor >= 1 ? (factor * 2) : factor
            cameraManager.setZoom(CGFloat(factor))
            print(CGFloat(factor))
        }
    

    获取当前设备(这部分为就代码,为Objective-C实现)

    // 根据位置查找摄像头
    - (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition) position {
        
        if (@available(iOS 13.0, *)) {
            AVCaptureDeviceDiscoverySession * session = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInTripleCamera, AVCaptureDeviceTypeBuiltInTrueDepthCamera, AVCaptureDeviceTypeBuiltInDualWideCamera, AVCaptureDeviceTypeBuiltInDualCamera, AVCaptureDeviceTypeBuiltInUltraWideCamera, AVCaptureDeviceTypeBuiltInTelephotoCamera, AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
    //        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInTripleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
            NSArray *devices = session.devices;
            AVCaptureDevice *deviceResult = nil;
            for (AVCaptureDevice *device in devices) {
                if ([device position] == position) {
                    deviceResult = device;
                    break;
                }
            }
            return deviceResult;
        } else {
            NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
            for (AVCaptureDevice *device in devices) {
                if ([device position] == position) {
                    return device;
                }
            }
        }
        return nil;
    }
    
    

    相关文章

      网友评论

          本文标题:相机调整焦距 0.5x 1x 2.0x 2.5x 3x的获取

          本文链接:https://www.haomeiwen.com/subject/ebukwrtx.html