OpenCV Camera rotation landscape left and right in iOS Swift

asked 2020-06-08 06:25:58 -0600

HMI gravatar image

updated 2020-06-08 07:48:52 -0600

supra56 gravatar image

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
edit retag flag offensive close merge delete

Comments

how is this related to opencv ? (sounds all off-topic to me)

berak gravatar imageberak ( 2020-06-08 07:17:09 -0600 )edit

videoCamera is object of CvVideoCamera. Am not clear about your comment

HMI gravatar imageHMI ( 2020-06-08 07:45:18 -0600 )edit