iOS大咖说

iOS开发 iPhone14 Pro/Pro Max后置摄像头近

2022-12-20  本文已影响0人  Leoeoo

一、解决方案

iPhone14 Pro/Pro Max的主摄焦距更长。在近距离拍摄时无法对焦。只需要判断机型然后更换焦距更短的摄像头即可。该摄像头与主摄相比,同样的距离会拍摄到更多的内容,此时可以通过调整摄像头的缩放来达到与主摄一样的效果(下面代码没有缩放)。

#import <sys/utsname.h>
#import <sys/stat.h>

- (AVCaptureDevice *)captureDeviceWithPosition:(AVCaptureDevicePosition)position {
    if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {
        AVCaptureDeviceType deviceType = AVCaptureDeviceTypeBuiltInWideAngleCamera;
        if (position == AVCaptureDevicePositionBack) {
            struct utsname systemInfo;
            uname(&systemInfo);
            NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
            if ([deviceModel isEqualToString:@"iPhone15,2"] ||
                [deviceModel isEqualToString:@"iPhone15,3"]) {
                if ([UIDevice currentDevice].systemVersion.floatValue >= 13.0) {
                    deviceType = AVCaptureDeviceTypeBuiltInUltraWideCamera;
                }
            }
        }
        AVCaptureDeviceDiscoverySession *devicesIOS10 = [AVCaptureDeviceDiscoverySession  discoverySessionWithDeviceTypes:@[deviceType] mediaType:AVMediaTypeVideo position:position];
        NSArray *devicesIOS  = devicesIOS10.devices;
        for (AVCaptureDevice *device in devicesIOS) {
            if ([device position] == position) {
                return device;
            }
        }
        return nil;
    } else {
        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        //遍历这些设备返回跟position相关的设备
        for (AVCaptureDevice *device in devices) {
            if ([device position] == position) {
                return device;
            }
        }
        return nil;
    }
}

二、AVCaptureDeviceType介绍

1、AVCaptureDeviceType

类型 介绍
AVCaptureDeviceTypeBuiltInMichrophone 麦克风设备
AVCaptureDeviceTypeBuiltInWideAngleCamera 内置广角相机设备
AVCaptureDeviceTypeBuiltInTelephotoCamera 内置的相机设备 焦距比广角相机长。注意: 此类设备只能使用 AVCaptureDeviceDiscoverySession 发现
AVCaptureDeviceTypeBuiltInUltraWideCamera 比广角相机焦距短的内置相机设备。注意: 此类设备只能使用 AVCaptureDeviceDiscoverySession 发现
AVCaptureDeviceTypeBuiltInDualCamera 一种由两个固定的焦距照相机组成的设备。一个是广角镜头(Wide),一个是远摄镜头 (Telephoto)
AVCaptureDeviceTypeBuiltInDualWideCamera 一种由两个固定焦距照相机组成的设备。一个是超宽镜头(Ultra Wide),一个是广角镜头(Wide Angle)
AVCaptureDeviceTypeBuiltInTripleCamera 一种由三个固定焦距照相机组成的设备。一个超宽镜头(Ultra Wide),一个广角镜头(Wide Angle)和一个远摄镜头(Telephoto)组成
AVCaptureDeviceTypeBuiltInTrueDepthCamera 一种由两台摄像机组成的设备。一台 YUV 和一台红外线。红外线摄像头可提供高质的深度信息,该信息可与 YUV 摄像头产生的帧同步并进行透视纠正。两台摄像头的分辨率可能不通透,但他们的相同的纵横比

2、对焦

类型
AVCaptureDeviceTypeBuiltInWideAngleCamera iPhone14 Pro/Pro Max后摄不能近距离对焦
AVCaptureDeviceTypeBuiltInUltraWideCamera iPhone14 Pro/Pro Max后摄可近距离对焦
AVCaptureDeviceTypeBuiltInDualWideCamera iPhone14 Pro/Pro Max后摄可近距离对焦
AVCaptureDeviceTypeBuiltInTripleCamera iPhone14 Pro/Pro Max后摄可近距离对焦 该类型只有iPhone14 Pro/Max能获取到

iPhone13可用设备:

(
    "<AVCaptureFigVideoDevice: 0x10508d000 [Back Camera][com.apple.avfoundation.avcapturedevice.built-in_video:0]>",
    "<AVCaptureFigVideoDevice: 0x105015200 [Back Ultra Wide Camera][com.apple.avfoundation.avcapturedevice.built-in_video:5]>",
    "<AVCaptureFigVideoDevice: 0x10508d600 [Back Dual Wide Camera][com.apple.avfoundation.avcapturedevice.built-in_video:6]>"
)

iPhone14 Pro/Pro Max可用设备:

(
    "<AVCaptureFigVideoDevice: 0x11706f200 [Back Camera][com.apple.avfoundation.avcapturedevice.built-in_video:0]>",
    "<AVCaptureFigVideoDevice: 0x11706f800 [Back Telephoto Camera][com.apple.avfoundation.avcapturedevice.built-in_video:2]>",
    "<AVCaptureFigVideoDevice: 0x117075200 [Back Ultra Wide Camera][com.apple.avfoundation.avcapturedevice.built-in_video:5]>",
    "<AVCaptureFigVideoDevice: 0x117075800 [Back Dual Camera][com.apple.avfoundation.avcapturedevice.built-in_video:3]>",
    "<AVCaptureFigVideoDevice: 0x117077000 [Back Dual Wide Camera][com.apple.avfoundation.avcapturedevice.built-in_video:6]>",
    "<AVCaptureFigVideoDevice: 0x117077600 [Back Triple Camera][com.apple.avfoundation.avcapturedevice.built-in_video:7]>",
    "<AVCaptureFigVideoDevice: 0x11707d000 [Back LiDAR Depth Camera][com.apple.avfoundation.avcapturedevice.built-in_video:9]>"
)
上一篇下一篇

猜你喜欢

热点阅读