gpt4 book ai didi

ios - 无法从 iPhone X 上的双摄像头拍摄双张照片

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:33 24 4
gpt4 key购买 nike

我正在尝试同时从 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'

是否需要启用或禁用其他设置?

最佳答案

您必须确保正确配置您的捕获设备、捕获 session 和捕获输出:

  1. 获取捕获设备,使用以下设置(这些设置在您的代码中已经正确):AVCaptureDeviceTypeBuiltInDualCamera、AVMediaTypeVideo、AVCaptureDevicePositionBack

  2. 使用您刚刚在 1 中检索到的设备创建 AVCaptureDeviceInput。

  3. 创建 AVCaptureSession 并将其 sessionPreset 设置为 AVCaptureSessionPresetPhoto
  4. 创建 AVCapturePhotoOutput
  5. 将创建的AVCaptureDeviceInput和AVCapturePhotoOutput添加到AVCaptureSession中
  6. 将 AVCapturePhotoOutput 的 dualCameraDualPhotoDeliveryEnabled 设置为 YES
  7. 开始捕获 session

对应代码(Objective-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];
});
  1. 稍后,对于您将从 AVCapturePhotoOutput 请求的每张照片,使用 AVCapturePhotoSettings 并将 dualCameraDualPhotoDeliveryEnabled 设置为 YES

代码( objective-C ):

AVCapturePhotoSettings* settings = [AVCapturePhotoSettings photoSettingsWithFormat:@{AVVideoCodecKey: AVVideoCodecTypeJPEG}];
settings.dualCameraDualPhotoDeliveryEnabled = YES;
[self.captureOutput capturePhotoWithSettings:settings delegate:self];

关于ios - 无法从 iPhone X 上的双摄像头拍摄双张照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47243449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com