自定义相机AVCaptureDevice近距离拍照模糊,无法聚焦问题。
问题描述
最近在写iOS身份证识别demo的时候发现,使用iPhone14Pro近距离识别的时候,身份证距离手机摄像头20CM以内,非常模糊根本看不清,各种办法都尝试了就是不行。
问题分析、决绝:
在 iPhone 14 Pro 上,近距离拍摄时,系统会自动切换到微距模式,这通常是使用超广角镜头(Ultra Wide Camera)来实现的。具体来说:
1. 主镜头(Wide Angle Camera):焦距通常适用于中等距离(通常约为 20-30 cm),如果拍摄距离太近,主镜头可能无法对焦清晰。
2. 超广角镜头(Ultra Wide Camera):焦距更近,能够支持更短的拍摄距离(通常大约 2-5 cm),因此在进行近距离拍摄时,系统会自动切换到这个镜头。
3. 远摄镜头(Telephoto Camera):用于远距离拍摄,不适合近距离对焦。
因此,在近距离拍摄时,iPhone 14 Pro 会使用 超广角镜头 来实现微距拍摄。如果你使用的是第三方应用(比如自己开发的应用),并且没有实现镜头切换功能,可能会遇到无法自动切换镜头的问题。
代码展示OC
- (AVCaptureDevice *)device {
if (_device == nil) {
// 获取所有可用的摄像头(包括主镜头、超广角镜头和远摄镜头)
NSArray *devices = [AVCaptureDeviceDiscoverySession
discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera,
AVCaptureDeviceTypeBuiltInUltraWideCamera,
AVCaptureDeviceTypeBuiltInTelephotoCamera]
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionBack].devices;
// 遍历摄像头设备,选择超广角镜头
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]) {
// 进行相关的配置(例如对焦、曝光、白平衡等)
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];
}
// 设置为选中的超广角镜头
_device = selectedDevice;
} else {
NSLog(@"未找到超广角镜头");
}
}
return _device;
}
- (AVCaptureSession *)session {
if (_session == nil) {
_session = [[AVCaptureSession alloc] init];
_session.sessionPreset = AVCaptureSessionPresetHigh;
NSError *error = nil;
// 使用选中的超广角镜头作为输入设备
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:&error];
if (error) {
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[self alertControllerWithTitle:@"没有摄像设备" 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];
// 设置需要检测的元数据类型,例如 AVMetadataObjectTypeFace(人脸)、AVMetadataObjectTypeQRCode(二维码)等。
self.metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeFace];
}
}
}
return _session;
}
代码展示Swift
var device: AVCaptureDevice? {
if _device == nil {
// 获取所有可用摄像头(包括主镜头、超广角镜头和远摄镜头)
let discoverySession = AVCaptureDevice.DiscoverySession(
deviceTypes: [.builtInWideAngleCamera, .builtInUltraWideCamera, .builtInTelephotoCamera],
mediaType: .video,
position: .back
)
let devices = discoverySession.devices
// 遍历摄像头设备,选择超广角镜头
var selectedDevice: AVCaptureDevice?
for device in devices {
if device.deviceType == .builtInUltraWideCamera {
selectedDevice = device
break
}
}
// 如果找到超广角镜头
if let selectedDevice = selectedDevice {
do {
try selectedDevice.lockForConfiguration()
// 配置自动对焦、曝光和白平衡等
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 {
// 使用选中的超广角镜头作为输入设备
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)
// 设置需要检测的元数据类型,例如人脸检测或二维码检测
metadataOutput.metadataObjectTypes = [.face]
}
} catch {
showAlert(title: "没有摄像设备", message: error.localizedDescription)
}
}
return _session
}