iOS 自定義相機(jī)近距離拍照模糊,無法自動(dòng)聚焦。

自定義相機(jī)AVCaptureDevice近距離拍照模糊,無法聚焦問題。

問題描述

最近在寫iOS身份證識(shí)別demo的時(shí)候發(fā)現(xiàn),使用iPhone14Pro近距離識(shí)別的時(shí)候,身份證距離手機(jī)攝像頭20CM以內(nèi),非常模糊根本看不清,各種辦法都嘗試了就是不行。

問題分析、決絕:

在 iPhone 14 Pro 上,近距離拍攝時(shí),系統(tǒng)會(huì)自動(dòng)切換到微距模式,這通常是使用超廣角鏡頭(Ultra Wide Camera)來實(shí)現(xiàn)的。具體來說:
1.  主鏡頭(Wide Angle Camera):焦距通常適用于中等距離(通常約為 20-30 cm),如果拍攝距離太近,主鏡頭可能無法對(duì)焦清晰。
2.  超廣角鏡頭(Ultra Wide Camera):焦距更近,能夠支持更短的拍攝距離(通常大約 2-5 cm),因此在進(jìn)行近距離拍攝時(shí),系統(tǒng)會(huì)自動(dòng)切換到這個(gè)鏡頭。
3.  遠(yuǎn)攝鏡頭(Telephoto Camera):用于遠(yuǎn)距離拍攝,不適合近距離對(duì)焦。
因此,在近距離拍攝時(shí),iPhone 14 Pro 會(huì)使用 超廣角鏡頭 來實(shí)現(xiàn)微距拍攝。如果你使用的是第三方應(yīng)用(比如自己開發(fā)的應(yīng)用),并且沒有實(shí)現(xiàn)鏡頭切換功能,可能會(huì)遇到無法自動(dòng)切換鏡頭的問題。

代碼展示OC

- (AVCaptureDevice *)device {
    if (_device == nil) {
        // 獲取所有可用的攝像頭(包括主鏡頭、超廣角鏡頭和遠(yuǎn)攝鏡頭)
        NSArray *devices = [AVCaptureDeviceDiscoverySession
                            discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera, 
                                                              AVCaptureDeviceTypeBuiltInUltraWideCamera, 
                                                              AVCaptureDeviceTypeBuiltInTelephotoCamera]
                            mediaType:AVMediaTypeVideo
                            position:AVCaptureDevicePositionBack].devices;
        
        // 遍歷攝像頭設(shè)備,選擇超廣角鏡頭
        AVCaptureDevice *selectedDevice = nil;
        for (AVCaptureDevice *device in devices) {
            if ([device.deviceType isEqualToString:AVCaptureDeviceTypeBuiltInUltraWideCamera]) {
                selectedDevice = device;
                break;
            }
        }
        
        // 如果找到了超廣角鏡頭
        if (selectedDevice) {
            NSError *error = nil;
            if ([selectedDevice lockForConfiguration:&error]) {
                // 進(jìn)行相關(guān)的配置(例如對(duì)焦、曝光、白平衡等)
                if ([selectedDevice isSmoothAutoFocusSupported]) {
                    selectedDevice.smoothAutoFocusEnabled = YES;
                }
                if ([selectedDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
                    selectedDevice.focusMode = AVCaptureFocusModeContinuousAutoFocus;
                }
                if ([selectedDevice isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
                    selectedDevice.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
                }
                if ([selectedDevice isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance]) {
                    selectedDevice.whiteBalanceMode = AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance;
                }
                [selectedDevice unlockForConfiguration];
            }
            
            // 設(shè)置為選中的超廣角鏡頭
            _device = selectedDevice;
        } else {
            NSLog(@"未找到超廣角鏡頭");
        }
    }
    
    return _device;
}

- (AVCaptureSession *)session {
    if (_session == nil) {
        _session = [[AVCaptureSession alloc] init];
        _session.sessionPreset = AVCaptureSessionPresetHigh;
        
        NSError *error = nil;
        // 使用選中的超廣角鏡頭作為輸入設(shè)備
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:&error];
        
        if (error) {
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];
            [self alertControllerWithTitle:@"沒有攝像設(shè)備" message:error.localizedDescription okAction:okAction cancelAction:nil];
        } else {
            if ([_session canAddInput:input]) {
                [_session addInput:input];
            }
            
            if ([_session canAddOutput:self.videoDataOutput]) {
                [_session addOutput:self.videoDataOutput];
            }
            
            if ([_session canAddOutput:self.metadataOutput]) {
                [_session addOutput:self.metadataOutput];
                // 設(shè)置需要檢測(cè)的元數(shù)據(jù)類型,例如 AVMetadataObjectTypeFace(人臉)、AVMetadataObjectTypeQRCode(二維碼)等。
                self.metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeFace];
            }
        }
    }
    
    return _session;
}

代碼展示Swift

var device: AVCaptureDevice? {
    if _device == nil {
        // 獲取所有可用攝像頭(包括主鏡頭、超廣角鏡頭和遠(yuǎn)攝鏡頭)
        let discoverySession = AVCaptureDevice.DiscoverySession(
            deviceTypes: [.builtInWideAngleCamera, .builtInUltraWideCamera, .builtInTelephotoCamera],
            mediaType: .video,
            position: .back
        )
        let devices = discoverySession.devices
        
        // 遍歷攝像頭設(shè)備,選擇超廣角鏡頭
        var selectedDevice: AVCaptureDevice?
        for device in devices {
            if device.deviceType == .builtInUltraWideCamera {
                selectedDevice = device
                break
            }
        }
        
        // 如果找到超廣角鏡頭
        if let selectedDevice = selectedDevice {
            do {
                try selectedDevice.lockForConfiguration()
                // 配置自動(dòng)對(duì)焦、曝光和白平衡等
                if selectedDevice.isSmoothAutoFocusSupported {
                    selectedDevice.isSmoothAutoFocusEnabled = true
                }
                if selectedDevice.isFocusModeSupported(.continuousAutoFocus) {
                    selectedDevice.focusMode = .continuousAutoFocus
                }
                if selectedDevice.isExposureModeSupported(.continuousAutoExposure) {
                    selectedDevice.exposureMode = .continuousAutoExposure
                }
                if selectedDevice.isWhiteBalanceModeSupported(.continuousAutoWhiteBalance) {
                    selectedDevice.whiteBalanceMode = .continuousAutoWhiteBalance
                }
                selectedDevice.unlockForConfiguration()
            } catch {
                print("配置超廣角鏡頭失敗:\(error)")
            }
            _device = selectedDevice
        } else {
            print("未找到超廣角鏡頭")
        }
    }
    return _device
}

var session: AVCaptureSession? {
    if _session == nil {
        _session = AVCaptureSession()
        _session?.sessionPreset = .high
        
        do {
            // 使用選中的超廣角鏡頭作為輸入設(shè)備
            let input = try AVCaptureDeviceInput(device: device ?? AVCaptureDevice())
            
            if _session?.canAddInput(input) == true {
                _session?.addInput(input)
            }
            
            if let videoOutput = videoDataOutput, _session?.canAddOutput(videoOutput) == true {
                _session?.addOutput(videoOutput)
            }
            
            if let metadataOutput = metadataOutput, _session?.canAddOutput(metadataOutput) == true {
                _session?.addOutput(metadataOutput)
                // 設(shè)置需要檢測(cè)的元數(shù)據(jù)類型,例如人臉檢測(cè)或二維碼檢測(cè)
                metadataOutput.metadataObjectTypes = [.face]
            }
        } catch {
            showAlert(title: "沒有攝像設(shè)備", message: error.localizedDescription)
        }
    }
    return _session
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容