iOS上的Double AVCaptureSession输出
问题描述:
我试图在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];
}
}
但是,当应用程序启动的前置摄像头的会议上,背部摄像头的会话停止,留下了静止图像。
有没有办法显示两台摄像机的输出?
谢谢
答
没有它没有。在使用AVCaptureSession时,一次只能使用一个摄像头。
不允许同时使用多个AVCaptureInput。所以只要一个会话开始,另一个会停止。
最好的办法是创建两个会话,首先开始第一个会话,一旦它报告一个帧,停止它并开始第二个会话。然后停下来,开始第一个,继续做下去。这将起作用,但您收到的输入中会有明显的延迟。
好的,但是可以在没有实时预览的情况下拍摄两个摄像头的图片吗? – 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