Ask Your Question
0

OpenCV setting iOS orientation

asked 2013-03-01 22:37:01 -0600

kaipyroami gravatar image

Is there anyway to stop openCV from rearranging camera input to align with the devices current orientation? I have an app that is set to a single orientation only, but as you move it around the openCV image keeps re aligning to match "right side up".

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2013-03-10 12:48:46 -0600

kaipyroami gravatar image

You need to override - (void)updateOrientation and - (void)layoutPreviewLayer.

CvVideoCameraMod.h

 #import <opencv2/highgui/cap_ios.h>

@protocol CvVideoCameraDelegateMod <CvVideoCameraDelegate>
@end

@interface CvVideoCameraMod : CvVideoCamera

- (void)updateOrientation;
- (void)layoutPreviewLayer;

@property (nonatomic, retain) CALayer *customPreviewLayer;

@end

CvVideoCameraMod.m

#import "CvVideoCameraMod.h"

 #define DEGREES_RADIANS(angle) ((angle) / 180.0 * M_PI)

@implementation CvVideoCameraMod

- (void)updateOrientation;
{
    NSLog(@"Would be rotating now... but I stopped it! :)");
    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 (defaultAVCaptureVideoOrientation) {
            case AVCaptureVideoOrientationLandscapeRight:
                rotation_angle = 180;
                break;
            case AVCaptureVideoOrientationPortraitUpsideDown:
                rotation_angle = 270;
                break;
            case AVCaptureVideoOrientationPortrait:
                rotation_angle = 90;
            case AVCaptureVideoOrientationLandscapeLeft:
                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 flag offensive delete link more

Comments

When I try implementing the code above, I get a warning about synthesizing the customPreviewLayer. In subsequent layoutPreviewLayer calls, customPreviewLayer is always nil. Do you have an example of this code being used?

Phando gravatar imagePhando ( 2013-11-26 16:07:45 -0600 )edit

instead of self.customPreviewLayer, use self->customPreviewLayer And you won't need the property, and therefore, you won't need to synthesize the object.

thelastinuit gravatar imagethelastinuit ( 2013-12-16 17:54:44 -0600 )edit

Also, check the rotation angle in CGAffineTransformMakeRotation( DEGREES_RADIANS(rotation_angle) ), you might need to change it.

thelastinuit gravatar imagethelastinuit ( 2013-12-16 17:56:22 -0600 )edit

Where does customPreviewLayer even come from? Is it the layer on the parentView?

JetForMe gravatar imageJetForMe ( 2014-04-05 18:20:34 -0600 )edit
0

answered 2015-06-15 01:13:11 -0600

Tamajit gravatar image

(Opencv 3.0) use this class

VideoCamera.h

#import <opencv2/videoio/cap_ios.h>

@protocol VideoCameraDelegate <CvVideoCameraDelegate>
@end

@interface VideoCamera : CvVideoCamera

- (void)updateOrientation;
- (void)layoutPreviewLayer;

@end

VideoCamera.mm

#import "VideoCamera.h"
#define DEGREES_RADIANS(angle) ((angle) / 180.0 * M_PI)
@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 (defaultAVCaptureVideoOrientation) {
            case AVCaptureVideoOrientationLandscapeRight:
                rotation_angle = 270;
                break;
            case AVCaptureVideoOrientationPortraitUpsideDown:
                rotation_angle = 180;
                break;
            case AVCaptureVideoOrientationLandscapeLeft:
                rotation_angle = 90;
                break;
            case AVCaptureVideoOrientationPortrait:
            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;
    }
}

------------- ViewController ----------

ViewController.h

#import "VideoCamera.h"
@interface ViewController : UIViewController<VideoCameraDelegate>{
    VideoCamera* videoCamera;

}
@property (nonatomic, strong) VideoCamera* videoCamera;

@property (strong, nonatomic) IBOutlet UIImageView *mainDisplay;

@end

ViewController.mm

#import "ViewController.h"
@interface ViewController ()

@end
@implementation ViewController

@synthesize videoCamera;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    videoCamera = [[VideoCamera alloc] initWithParentView:_mainDisplay];
    videoCamera.delegate = self;
    videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
    videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPresetHigh;
    videoCamera.defaultAVCaptureVideoOrientation =  AVCaptureVideoOrientationPortrait;
    videoCamera.defaultFPS = 30;
    [videoCamera start];//to start camera preview
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)processImage:(cv::Mat&)image
{
      //process here
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-03-01 22:37:01 -0600

Seen: 4,981 times

Last updated: Mar 10 '13