5

我正在尝试同时从 iOS 设备上的两个相机拍照。我还想在屏幕上实时预览两个摄像头。我使用这段代码:

- (void)prepareCameraView:(UIView *)window
{
    NSArray *captureDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

    {
        AVCaptureSession *session = [[AVCaptureSession alloc] init];
        session.sessionPreset = AVCaptureSessionPresetMedium;

        CALayer *viewLayer = window.layer;
        NSLog(@"viewLayer = %@", viewLayer);

        AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
        captureVideoPreviewLayer.frame = CGRectMake(0.0f, 0.0f, window.bounds.size.width/2.0f, window.bounds.size.height);
        [window.layer addSublayer:captureVideoPreviewLayer];

        NSError *error = nil;
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:[captureDevices objectAtIndex:0] error:&error];
        if (!input)
        {
            NSLog(@"ERROR : trying to open camera : %@", error);
        }

        [session addInput:input];

        [session startRunning];
    }

    {
        AVCaptureSession *session = [[AVCaptureSession alloc] init];
        session.sessionPreset = AVCaptureSessionPresetMedium;

        CALayer *viewLayer = window.layer;
        NSLog(@"viewLayer = %@", viewLayer);

        AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
        captureVideoPreviewLayer.frame = CGRectMake(window.bounds.size.width/2.0f, 0.0f, window.bounds.size.width/2.0f, window.bounds.size.height);
        [window.layer addSublayer:captureVideoPreviewLayer];

        NSError *error = nil;
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:[captureDevices objectAtIndex:1] error:&error];
        if (!input)
        {
            NSLog(@"ERROR : trying to open camera : %@", error);
        }

        [session addInput:input];

        [session startRunning];
    }

}

但是当应用程序启动前置摄像头的会话时,后置摄像头的会话会停止并留下静止图像。

有没有办法实时显示两个摄像机的输出?

谢谢

4

1 回答 1

2

不,这不对。使用 AVCaptureSession 时,一次只能使用一个摄像机源。

不允许同时使用多个 AVCaptureInput。因此,一旦一个会话开始,另一个会话就会停止。

您最好的选择是创建两个会话,启动第一个会话,并在报告帧后立即停止并启动第二个会话。然后停止第二个并开始第一个,继续这样做。这会起作用,但您收到的输入会有明显的延迟。

于 2013-03-16T11:21:50.087 回答