我试图同时从iPhoneX上的远摄和广角相机捕捉。这是我初始化设备的方式:
let captureDevice = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back)
我要求为AVPhotoOutput提供双张照片:
let photoSettings = AVCapturePhotoSettings()
photoSettings.isDualCameraDualPhotoDeliveryEnabled = true
capturePhotoOutput.capturePhoto(with: photoSettings, delegate: self)
但是,我遇到了这个错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCapturePhotoOutput setDualCameraDualPhotoDeliveryEnabled:] Dual Camera dual photo delivery is not supported in this configuration'
我需要启用或禁用其他设置吗?
答案 0 :(得分:0)
您必须确保正确配置了捕获设备,捕获会话和捕获输出:
使用以下设置(在您的代码中已经正确)获取捕获设备:AVCaptureDeviceTypeBuiltInDualCamera,AVMediaTypeVideo,AVCaptureDevicePositionBack
使用您刚刚在1中检索到的设备创建AVCaptureDeviceInput。
对应的代码(目标C):
// Create capture device discovery session to retrieve devices matching our
// needs
// -------------------------------------------------------------------------------
AVCaptureDeviceDiscoverySession* captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInDualCamera]
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionBack];
// Loop through the retrieved devices and select correct one
// -------------------------------------------------------------------------------
for(AVCaptureDevice* device in [captureDeviceDiscoverySession devices])
{
if(device.position == AVCaptureDevicePositionBack)
{
self.captureDevice = device;
break;
}
}
// Get camera input
// -------------------------------------------------------------------------------
NSError* error = nil;
AVCaptureDeviceInput* videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:self.captureDevice error:&error];
if(!videoDeviceInput)
{
NSLog(@"Could not retrieve camera input, error: %@", error);
return;
}
// Initialize capture session
// -------------------------------------------------------------------------------
self.captureSession = [[AVCaptureSession alloc] init];
self.captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
// Add video device input and photo data output to our capture session
// -------------------------------------------------------------------------------
self.captureOutput = [AVCapturePhotoOutput new];
[self.captureSession beginConfiguration];
if(![self.captureSession canAddOutput:self.captureOutput])
{
NSLog(@"Cannot add photo output!");
return;
}
[self.captureSession addInput:videoDeviceInput];
[self.captureSession addOutput:self.captureOutput];
[self.captureSession commitConfiguration];
// Configure output settings AFTER input & output have been added to the session
// -------------------------------------------------------------------------------
if(self.captureOutput.isDualCameraDualPhotoDeliverySupported)
self.captureOutput.dualCameraDualPhotoDeliveryEnabled = YES;
// Create video preview layer for this session, and add it to our preview UIView
// -------------------------------------------------------------------------------
AVCaptureVideoPreviewLayer* videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspect;
videoPreviewLayer.frame = self.view.bounds;
[self.view.layer addSublayer:videoPreviewLayer];
// Start capturing session
// -------------------------------------------------------------------------------
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[self.captureSession startRunning];
});
代码(目标C):
AVCapturePhotoSettings* settings = [AVCapturePhotoSettings photoSettingsWithFormat:@{AVVideoCodecKey: AVVideoCodecTypeJPEG}];
settings.dualCameraDualPhotoDeliveryEnabled = YES;
[self.captureOutput capturePhotoWithSettings:settings delegate:self];