【问题标题】:Double AVCaptureSession output on iOSiOS 上的双 AVCaptureSession 输出
【发布时间】:2013-03-05 02:45:18
【问题描述】:

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

}

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

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

谢谢

【问题讨论】:

    标签: ios objective-c avfoundation avcapturesession


    【解决方案1】:

    不,不是。使用 AVCaptureSession 时,一次只能使用一个相机源。

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

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

    【讨论】:

    • 好的,但是是否可以在没有实时预览的情况下同时拍摄两个相机的照片?
    • 我认为最好只运行一个会话并使用 [AVsession beginConfiguration] 切换相机; [AVsession addInput:inputCam]; [AVsession 提交配置];不过,您仍然会有一些延迟
    • 虽然它可能有效,但我可以看到这样的解决方案太慢了,无法使用。更好的解决方案是使用 AVCaptureVideoDataOutput 并使用 AVCaptureVideoDataOutputSampleBufferDelegate 绘制输出。此解决方案在此处详细说明:stackoverflow.com/questions/16543075/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 2014-01-11
    相关资源
    最近更新 更多