Ask Your Question

Revision history [back]

OpenCV Camera rotation landscape left and right in iOS Swift

I used openCV camera to track the objects. The code is as below and defaultAVCaptureVideoOrientation is set to landscape RIGHT. Now if i rotate the device to landscape LEFT, the camera is not rotated correctly. So i used layoutPreviewLayer to set the camera rotation but it is not working.

self.videoCamera = [[VideoCamera alloc] initWithParentView:imgView];
self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationLandscapeRight;
self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
self.videoCamera.rotateVideo = YES;

self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset640x480;

self.videoCamera.defaultFPS = 30;
self.videoCamera.delegate = self;
self.videoCamera.recordVideo = NO;
self.videoCamera.grayscaleMode = NO;

Using the below code the landscape RIGHT is working fine. But if i rotate the iphoneXR device to landscape LEFT, it is not working.

@implementation VideoCamera - (void)updateOrientation { self->customPreviewLayer.bounds = CGRectMake(0, 0, self.parentView.frame.size.width, self.parentView.frame.size.height); [self layoutPreviewLayer]; }

  • (void)layoutPreviewLayer { if (self.parentView != nil) { CALayer* layer = self->customPreviewLayer; CGRect bounds = self->customPreviewLayer.bounds; int rotation_angle = 0;

    switch (self.defaultAVCaptureVideoOrientation) {
        case AVCaptureVideoOrientationLandscapeRight:
            rotation_angle = -180;
            break;
        case AVCaptureVideoOrientationLandscapeLeft:
            rotation_angle = 180;
            break;
        default:
            break;
    }
    layer.position = CGPointMake(self.parentView.frame.size.width/2., self.parentView.frame.size.height/2.);
    layer.affineTransform = CGAffineTransformMakeRotation( DEGREES_RADIANS(rotation_angle) );
    layer.bounds = bounds;
    

    } } @end

click to hide/show revision 2
retagged

updated 2020-06-08 07:14:49 -0600

berak gravatar image

OpenCV Camera rotation landscape left and right in iOS Swift

I used openCV camera to track the objects. The code is as below and defaultAVCaptureVideoOrientation is set to landscape RIGHT. Now if i rotate the device to landscape LEFT, the camera is not rotated correctly. So i used layoutPreviewLayer to set the camera rotation but it is not working.

self.videoCamera = [[VideoCamera alloc] initWithParentView:imgView];
self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationLandscapeRight;
self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
self.videoCamera.rotateVideo = YES;

self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset640x480;

self.videoCamera.defaultFPS = 30;
self.videoCamera.delegate = self;
self.videoCamera.recordVideo = NO;
self.videoCamera.grayscaleMode = NO;

Using the below code the landscape RIGHT is working fine. But if i rotate the iphoneXR device to landscape LEFT, it is not working.

@implementation VideoCamera - (void)updateOrientation { self->customPreviewLayer.bounds = CGRectMake(0, 0, self.parentView.frame.size.width, self.parentView.frame.size.height); [self layoutPreviewLayer]; }

  • (void)layoutPreviewLayer { if (self.parentView != nil) { CALayer* layer = self->customPreviewLayer; CGRect bounds = self->customPreviewLayer.bounds; int rotation_angle = 0;

    switch (self.defaultAVCaptureVideoOrientation) {
        case AVCaptureVideoOrientationLandscapeRight:
            rotation_angle = -180;
            break;
        case AVCaptureVideoOrientationLandscapeLeft:
            rotation_angle = 180;
            break;
        default:
            break;
    }
    layer.position = CGPointMake(self.parentView.frame.size.width/2., self.parentView.frame.size.height/2.);
    layer.affineTransform = CGAffineTransformMakeRotation( DEGREES_RADIANS(rotation_angle) );
    layer.bounds = bounds;
    

    } } @end

OpenCV Camera rotation landscape left and right in iOS Swift

I used openCV camera to track the objects. The code is as below and defaultAVCaptureVideoOrientation is set to landscape RIGHT. Now if i rotate the device to landscape LEFT, the camera is not rotated correctly. So i used layoutPreviewLayer to set the camera rotation but it is not working.

self.videoCamera = [[VideoCamera alloc] initWithParentView:imgView];
self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationLandscapeRight;
self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
self.videoCamera.rotateVideo = YES;

self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset640x480;

self.videoCamera.defaultFPS = 30;
self.videoCamera.delegate = self;
self.videoCamera.recordVideo = NO;
self.videoCamera.grayscaleMode = NO;

Using the below code the landscape RIGHT is working fine. But if i rotate the iphoneXR device to landscape LEFT, it is not working.

@implementation VideoCamera
- (void)updateOrientation
{
    self->customPreviewLayer.bounds = CGRectMake(0, 0, self.parentView.frame.size.width, self.parentView.frame.size.height);
    [self layoutPreviewLayer];
}

  • } - (void)layoutPreviewLayer { if (self.parentView != nil) { CALayer* layer = self->customPreviewLayer; CGRect bounds = self->customPreviewLayer.bounds; int rotation_angle = 0;

    0;
    
            switch (self.defaultAVCaptureVideoOrientation) {
         case AVCaptureVideoOrientationLandscapeRight:
             rotation_angle = -180;
             break;
         case AVCaptureVideoOrientationLandscapeLeft:
             rotation_angle = 180;
             break;
         default:
             break;
     }
     layer.position = CGPointMake(self.parentView.frame.size.width/2., self.parentView.frame.size.height/2.);
     layer.affineTransform = CGAffineTransformMakeRotation( DEGREES_RADIANS(rotation_angle) );
     layer.bounds = bounds;
        }
    }
    @end
    

    } } @end