2013-03-16 59 views
4

我試圖在iOS設備上同時拍攝兩張照片。我也想在屏幕上預覽兩臺相機。我用這個代碼:iOS上的Double AVCaptureSession輸出

- (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]; 
    } 

} 

但是,當應用程序啓動的前置攝像頭的會議上,背部攝像頭的會話停止,留下了靜止圖像。

有沒有辦法顯示兩臺攝像機的輸出?

謝謝

回答

-1

沒有它沒有。在使用AVCaptureSession時,一次只能使用一個攝像頭。

不允許同時使用多個AVCaptureInput。所以只要一個會話開始,另一個會停止。

最好的辦法是創建兩個會話,首先開始第一個會話,一旦它報告一個幀,停止它並開始第二個會話。然後停下來,開始第一個,繼續做下去。這將起作用,但您收到的輸入中會有明顯的延遲。

+0

好的,但是可以在沒有實時預覽的情況下拍攝兩個攝像頭的圖片嗎? – Abel 2013-03-16 11:30:07

+2

我認爲最好只有一個會話一直運行,並用[AVsession beginConfiguration]切換攝像頭; [AVsession addInput:inputCam]; [AVsession commitConfiguration]; 儘管可能會起作用,但您仍然會有一些延遲 – Sten 2013-03-16 16:10:55

+0

雖然可能有效,但我可以看到此解決方案如此緩慢,無法使用。更好的解決方案是使用AVCaptureVideoDataOutput並使用AVCaptureVideoDataOutputSampleBufferDelegate繪製輸出。此解決方案在此處詳述:http://stackoverflow.com/questions/16543075/avcapturesession-with-multiple-previews/25167597#25167597 – Johnny 2014-08-07 18:24:38