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