2 * by Eduard Feicho on 29/07/12
3 * Copyright 2012. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #import <UIKit/UIKit.h>
30 #import <Accelerate/Accelerate.h>
31 #import <AVFoundation/AVFoundation.h>
32 #import <ImageIO/ImageIO.h>
33 #include "opencv2/core/core.hpp"
35 /////////////////////////////////////// CvAbstractCamera /////////////////////////////////////
37 @class CvAbstractCamera;
39 @interface CvAbstractCamera : NSObject
41 AVCaptureSession* captureSession;
42 AVCaptureConnection* videoCaptureConnection;
43 AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;
45 UIDeviceOrientation currentDeviceOrientation;
48 BOOL captureSessionLoaded;
50 BOOL useAVCaptureVideoPreviewLayer;
52 AVCaptureDevicePosition defaultAVCaptureDevicePosition;
53 AVCaptureVideoOrientation defaultAVCaptureVideoOrientation;
54 NSString *const defaultAVCaptureSessionPreset;
64 @property (nonatomic, retain) AVCaptureSession* captureSession;
65 @property (nonatomic, retain) AVCaptureConnection* videoCaptureConnection;
67 @property (nonatomic, readonly) BOOL running;
68 @property (nonatomic, readonly) BOOL captureSessionLoaded;
70 @property (nonatomic, assign) int defaultFPS;
71 @property (nonatomic, assign) AVCaptureDevicePosition defaultAVCaptureDevicePosition;
72 @property (nonatomic, assign) AVCaptureVideoOrientation defaultAVCaptureVideoOrientation;
73 @property (nonatomic, assign) BOOL useAVCaptureVideoPreviewLayer;
74 @property (nonatomic, strong) NSString *const defaultAVCaptureSessionPreset;
76 @property (nonatomic, assign) int imageWidth;
77 @property (nonatomic, assign) int imageHeight;
79 @property (nonatomic, retain) UIView* parentView;
83 - (void)switchCameras;
85 - (id)initWithParentView:(UIView*)parent;
87 - (void)createCaptureOutput;
88 - (void)createVideoPreviewLayer;
89 - (void)updateOrientation;
94 - (void)unlockExposure;
96 - (void)unlockBalance;
100 ///////////////////////////////// CvVideoCamera ///////////////////////////////////////////
102 @class CvVideoCamera;
104 @protocol CvVideoCameraDelegate <NSObject>
107 // delegate method for processing image frames
108 - (void)processImage:(cv::Mat&)image;
113 @interface CvVideoCamera : CvAbstractCamera<AVCaptureVideoDataOutputSampleBufferDelegate>
115 AVCaptureVideoDataOutput *videoDataOutput;
117 dispatch_queue_t videoDataOutputQueue;
118 CALayer *customPreviewLayer;
124 AVAssetWriterInput* recordAssetWriterInput;
125 AVAssetWriterInputPixelBufferAdaptor* recordPixelBufferAdaptor;
126 AVAssetWriter* recordAssetWriter;
128 CMTime lastSampleTime;
132 @property (nonatomic, assign) id<CvVideoCameraDelegate> delegate;
133 @property (nonatomic, assign) BOOL grayscaleMode;
135 @property (nonatomic, assign) BOOL recordVideo;
136 @property (nonatomic, assign) BOOL rotateVideo;
137 @property (nonatomic, retain) AVAssetWriterInput* recordAssetWriterInput;
138 @property (nonatomic, retain) AVAssetWriterInputPixelBufferAdaptor* recordPixelBufferAdaptor;
139 @property (nonatomic, retain) AVAssetWriter* recordAssetWriter;
141 - (void)adjustLayoutToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
142 - (void)layoutPreviewLayer;
144 - (NSURL *)videoFileURL;
149 ///////////////////////////////// CvPhotoCamera ///////////////////////////////////////////
151 @class CvPhotoCamera;
153 @protocol CvPhotoCameraDelegate <NSObject>
155 - (void)photoCamera:(CvPhotoCamera*)photoCamera capturedImage:(UIImage *)image;
156 - (void)photoCameraCancel:(CvPhotoCamera*)photoCamera;
160 @interface CvPhotoCamera : CvAbstractCamera
162 AVCaptureStillImageOutput *stillImageOutput;
165 @property (nonatomic, assign) id<CvPhotoCameraDelegate> delegate;