2

I am trying to implement side by side two camera views for vr application. I found some useful info on at this site:

How to show 2 camera preview side by side?[For cardboard apps]

but I want how to create it with iOS?

I am trying to create same behaviour with AVFoundation. This is my current code

CameraViewController

#import <AVFoundation/AVFoundation.h>
#import "CameraViewController.h"

@interface CameraViewController ()

@property (strong, nonatomic) AVCaptureSession *captureSession;
@property (strong, nonatomic) AVCaptureStillImageOutput *stillImageOutput;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *previewLayerLeft;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *previewLayerRight;

@end

@implementation CameraViewController

#pragma mark - lazy instantiation

- (AVCaptureSession *)captureSession {
    if (!_captureSession)
        _captureSession = [[AVCaptureSession alloc] init];
    return _captureSession;
}

- (AVCaptureStillImageOutput *)stillImageOutput {
    if (!_stillImageOutput)
        _stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    return _stillImageOutput;
}

- (AVCaptureVideoPreviewLayer *)previewLayerLeft {
    if (!_previewLayerLeft)
        _previewLayerLeft = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
    return _previewLayerLeft;
}

- (AVCaptureVideoPreviewLayer *)previewLayerRight {
    if (!_previewLayerRight)
        _previewLayerRight = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
    return _previewLayerRight;
}

#pragma mark - view controller's life cycle methods

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
    NSError *error;
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] error:&error];

    if ([self.captureSession canAddInput:deviceInput])
        [self.captureSession addInput:deviceInput];

    [self.previewLayerLeft setVideoGravity:AVLayerVideoGravityResizeAspect];
    [self.previewLayerRight setVideoGravity:AVLayerVideoGravityResizeAspect];

    CALayer *rootLayer = [self.view layer];
    [rootLayer setMasksToBounds:YES];
    NSLog(@"%@", NSStringFromCGRect([rootLayer frame]));

    if ([self.previewLayerLeft.connection isVideoOrientationSupported])
        [self.previewLayerLeft.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; //home button on right. Refer to .h not doc

    [self.previewLayerLeft setFrame:CGRectMake(0, 0, [rootLayer frame].size.width / 2, [rootLayer frame].size.height)];

    if ([self.previewLayerRight.connection isVideoOrientationSupported])
        [self.previewLayerRight.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; //home button on right. Refer to .h not doc

    [self.previewLayerRight setFrame:CGRectMake([rootLayer frame].size.width / 2, 0, [rootLayer frame].size.width / 2, [rootLayer frame].size.height)];

    [rootLayer insertSublayer:self.previewLayerLeft atIndex:0];
    [rootLayer insertSublayer:self.previewLayerRight atIndex:1];

    [self.stillImageOutput setOutputSettings:[[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil]];

    if ([self.captureSession canAddOutput:self.stillImageOutput])
        [self.captureSession addOutput:self.stillImageOutput];

    [self.captureSession startRunning];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
Community
  • 1
  • 1
appleBoy21
  • 632
  • 8
  • 23

0 Answers0