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];
}
}
但是,當應用程序啓動的前置攝像頭的會議上,背部攝像頭的會話停止,留下了靜止圖像。
有沒有辦法顯示兩臺攝像機的輸出?
謝謝
好的,但是可以在沒有實時預覽的情況下拍攝兩個攝像頭的圖片嗎? – Abel 2013-03-16 11:30:07
我認爲最好只有一個會話一直運行,並用[AVsession beginConfiguration]切換攝像頭; [AVsession addInput:inputCam]; [AVsession commitConfiguration]; 儘管可能會起作用,但您仍然會有一些延遲 – Sten 2013-03-16 16:10:55
雖然可能有效,但我可以看到此解決方案如此緩慢,無法使用。更好的解決方案是使用AVCaptureVideoDataOutput並使用AVCaptureVideoDataOutputSampleBufferDelegate繪製輸出。此解決方案在此處詳述:http://stackoverflow.com/questions/16543075/avcapturesession-with-multiple-previews/25167597#25167597 – Johnny 2014-08-07 18:24:38