iPhone 7 Plus - AVFoundation双摄像头

时间:2016-09-08 07:35:46

标签: ios avfoundation

我现在正在积极研究这个问题,但现在iPhone 7 Plus配备了双摄像头系统,AVFoundation是否允许您同时处理来自每个特定摄像头的视频帧?

我在想/我希望能够在给定某个位置的同时处理来自两个AVCaptureDevice个实例的输出。

2 个答案:

答案 0 :(得分:14)

您当时只能将一台摄像机添加到AVCaptureSession。例如,您可以在前置和后置摄像头之间切换,但不能同时使用两者。与7 Plus上的两个后置摄像头相同,您必须选择其中之一。但是,由于你也可以打电话给一对二合一相机,因此会有一些小差异。在缩放时合并来自两个相机的图像。但这仅适用于静态照片,您只能获得一个图像/捕获缓冲区。对于视频,您必须选择任一相机。

要选择相机,您可以使用新的AVCaptureDeviceDiscoverySession。要使用二重奏相机:

@property (nonatomic) AVCaptureDevice *backCamera;
@property (nonatomic) AVCaptureDeviceInput *backCameraInput;


if([AVCaptureDeviceDiscoverySession class]){
    NSArray *allTypes = @[AVCaptureDeviceTypeBuiltInDuoCamera, AVCaptureDeviceTypeBuiltInWideAngleCamera, AVCaptureDeviceTypeBuiltInTelephotoCamera ];
    AVCaptureDeviceDiscoverySession *discoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:allTypes mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];

    for(AVCaptureDevice *device in discoverySession.devices) {
        if(device.deviceType== AVCaptureDeviceTypeBuiltInDuoCamera){
            self.backCamera = device;
            self.backCameraInput = [AVCaptureDeviceInput deviceInputWithDevice:self.backCamera error:&error];
        }
    }
}

if(!self.backCamera){
    self.backCamera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    self.backCameraInput = [AVCaptureDeviceInput deviceInputWithDevice:self.backCamera error:&error];
}

单独使用广角和远摄相机

@property (nonatomic) AVCaptureDevice *backCamera;
@property (nonatomic) AVCaptureDeviceInput *backCameraInput;
@property (nonatomic) AVCaptureDevice *teleCamera;
@property (nonatomic) AVCaptureDeviceInput *teleCameraInput;


if([AVCaptureDeviceDiscoverySession class]){
    NSArray *allTypes = @[AVCaptureDeviceTypeBuiltInDuoCamera, AVCaptureDeviceTypeBuiltInWideAngleCamera, AVCaptureDeviceTypeBuiltInTelephotoCamera ];
    AVCaptureDeviceDiscoverySession *discoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:allTypes mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];

    for(AVCaptureDevice *device in discoverySession.devices) {
        if(device.deviceType==AVCaptureDeviceTypeBuiltInWideAngleCamera){
            self.backCamera = device;
            self.backCameraInput = [AVCaptureDeviceInput deviceInputWithDevice:self.backCamera error:&error];
        }

        if(device.deviceType==AVCaptureDeviceTypeBuiltInTelephotoCamera){
            self.teleCamera = device;
            self.teleCameraInput = [AVCaptureDeviceInput deviceInputWithDevice:self.teleCamera error:&error];
        }
    }
}

if(!self.backCamera){
    self.backCamera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    self.backCameraInput = [AVCaptureDeviceInput deviceInputWithDevice:self.backCamera error:&error];
}

如果您不这样做,或保留旧代码,即使您放大,也只能使用广角相机。

编辑:在iOS 11中有一个名为dualCameraDualPhotoDeliveryEnabled的新AVCapturePhotoSettings。它允许您同时拍摄两张静止图像,但是没有流媒体/视频。

答案 1 :(得分:7)

在更新的AVFoundation文档(AVCaptureDeviceType)中,有新的设备类型:builtInWideAngleCamerabuiltInTelephotoCamera。因此,应该可以创建多个捕获会话并同时从它们获得反馈。