My CvVideoCamera
auto-rotates, and I would like it not to. This has been raised on the bugtracker, but there has been no activity for a while.
3 Answers
Subclass CvVideoCamera
, and just remove the rotation code. I wanted my orientation always to be portrait, so this was simple:
MyCvVideoCamera.h
#import <opencv2/highgui/cap_ios.h>
@interface MyCvVideoCamera : CvVideoCamera
- (void)updateOrientation;
- (void)layoutPreviewLayer;
@property (nonatomic, retain) CALayer *customPreviewLayer;
@end
MyCvVideoCamera.m
#import "MyCvVideoCamera.h"
@implementation MyCvVideoCamera
- (void)updateOrientation;
{
// nop
}
- (void)layoutPreviewLayer;
{
if (self.parentView != nil) {
CALayer* layer = self.customPreviewLayer;
CGRect bounds = self.customPreviewLayer.bounds;
layer.position = CGPointMake(self.parentView.frame.size.width/2., self.parentView.frame.size.height/2.);
layer.bounds = bounds;
}
}
@end
-
-
1@Luke Yes, I submitted them together. I spent ages researching this, and it seems to be a common enough problem, I thought others could potentially benefit.– fredleyFeb 14, 2013 at 14:38
-
Hi, how do you initialize myCvVideoCamera? I have it just like you, however, when my camera preview should be on, it is just blank, and I think the only problem is how I am initializing– IsaacMar 8, 2013 at 8:33
-
-
2When I tried your code above, I got the warning that the autosynthesized property (customPreviewLayer) would create its own member variable (_customPreviewLayer) rather than using the existing instance variable of the superclass. Since this clearly wouldn't work, I tried adding a \@synthesize customPreviewLayer = customPreviewLayer; line, but that creates an error. I ended up removing the property and changing self.customPreviewLayer to self->customPreviewLayer. Did you run into a similar problem, @tom-medley ? Oct 29, 2013 at 22:07
To make it work, I had to add:
@protocol CvVideoCameraDelegateMod <CvVideoCameraDelegate>
@end
to MyCvVideoCamera.h described above
and
@synthesize customPreviewLayer = _customPreviewLayer;
to MyCvVideoCamera.m
Quick fix Before initialize the CVVideoCamera add these lines
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
self.videoCamera = [[CvVideoCamera alloc] initWithParentView:self.previewImage]; self.videoCamera.delegate = self;