Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

(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
}