1 /* For iOS video I/O 2 * by Eduard Feicho on 29/07/12 3 * Copyright 2012. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 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. 15 * 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. 26 * 27 */ 28 29 #import <UIKit/UIKit.h> 30 #import <Accelerate/Accelerate.h> 31 #import <AVFoundation/AVFoundation.h> 32 #import <ImageIO/ImageIO.h> 33 #include "opencv2/core.hpp" 34 35 //! @addtogroup videoio_ios 36 //! @{ 37 38 /////////////////////////////////////// CvAbstractCamera ///////////////////////////////////// 39 40 @class CvAbstractCamera; 41 42 @interface CvAbstractCamera : NSObject 43 { 44 AVCaptureSession* captureSession; 45 AVCaptureConnection* videoCaptureConnection; 46 AVCaptureVideoPreviewLayer *captureVideoPreviewLayer; 47 48 UIDeviceOrientation currentDeviceOrientation; 49 50 BOOL cameraAvailable; 51 BOOL captureSessionLoaded; 52 BOOL running; 53 BOOL useAVCaptureVideoPreviewLayer; 54 55 AVCaptureDevicePosition defaultAVCaptureDevicePosition; 56 AVCaptureVideoOrientation defaultAVCaptureVideoOrientation; 57 NSString *const defaultAVCaptureSessionPreset; 58 59 int defaultFPS; 60 61 UIView* parentView; 62 63 int imageWidth; 64 int imageHeight; 65 } 66 67 @property (nonatomic, retain) AVCaptureSession* captureSession; 68 @property (nonatomic, retain) AVCaptureConnection* videoCaptureConnection; 69 70 @property (nonatomic, readonly) BOOL running; 71 @property (nonatomic, readonly) BOOL captureSessionLoaded; 72 73 @property (nonatomic, assign) int defaultFPS; 74 @property (nonatomic, assign) AVCaptureDevicePosition defaultAVCaptureDevicePosition; 75 @property (nonatomic, assign) AVCaptureVideoOrientation defaultAVCaptureVideoOrientation; 76 @property (nonatomic, assign) BOOL useAVCaptureVideoPreviewLayer; 77 @property (nonatomic, strong) NSString *const defaultAVCaptureSessionPreset; 78 79 @property (nonatomic, assign) int imageWidth; 80 @property (nonatomic, assign) int imageHeight; 81 82 @property (nonatomic, retain) UIView* parentView; 83 84 - (void)start; 85 - (void)stop; 86 - (void)switchCameras; 87 88 - (id)initWithParentView:(UIView*)parent; 89 90 - (void)createCaptureOutput; 91 - (void)createVideoPreviewLayer; 92 - (void)updateOrientation; 93 94 - (void)lockFocus; 95 - (void)unlockFocus; 96 - (void)lockExposure; 97 - (void)unlockExposure; 98 - (void)lockBalance; 99 - (void)unlockBalance; 100 101 @end 102 103 ///////////////////////////////// CvVideoCamera /////////////////////////////////////////// 104 105 @class CvVideoCamera; 106 107 @protocol CvVideoCameraDelegate <NSObject> 108 109 #ifdef __cplusplus 110 // delegate method for processing image frames 111 - (void)processImage:(cv::Mat&)image; 112 #endif 113 114 @end 115 116 @interface CvVideoCamera : CvAbstractCamera<AVCaptureVideoDataOutputSampleBufferDelegate> 117 { 118 AVCaptureVideoDataOutput *videoDataOutput; 119 120 dispatch_queue_t videoDataOutputQueue; 121 CALayer *customPreviewLayer; 122 123 BOOL grayscaleMode; 124 125 BOOL recordVideo; 126 BOOL rotateVideo; 127 AVAssetWriterInput* recordAssetWriterInput; 128 AVAssetWriterInputPixelBufferAdaptor* recordPixelBufferAdaptor; 129 AVAssetWriter* recordAssetWriter; 130 131 CMTime lastSampleTime; 132 133 } 134 135 @property (nonatomic, assign) id<CvVideoCameraDelegate> delegate; 136 @property (nonatomic, assign) BOOL grayscaleMode; 137 138 @property (nonatomic, assign) BOOL recordVideo; 139 @property (nonatomic, assign) BOOL rotateVideo; 140 @property (nonatomic, retain) AVAssetWriterInput* recordAssetWriterInput; 141 @property (nonatomic, retain) AVAssetWriterInputPixelBufferAdaptor* recordPixelBufferAdaptor; 142 @property (nonatomic, retain) AVAssetWriter* recordAssetWriter; 143 144 - (void)adjustLayoutToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; 145 - (void)layoutPreviewLayer; 146 - (void)saveVideo; 147 - (NSURL *)videoFileURL; 148 149 150 @end 151 152 ///////////////////////////////// CvPhotoCamera /////////////////////////////////////////// 153 154 @class CvPhotoCamera; 155 156 @protocol CvPhotoCameraDelegate <NSObject> 157 158 - (void)photoCamera:(CvPhotoCamera*)photoCamera capturedImage:(UIImage *)image; 159 - (void)photoCameraCancel:(CvPhotoCamera*)photoCamera; 160 161 @end 162 163 @interface CvPhotoCamera : CvAbstractCamera 164 { 165 AVCaptureStillImageOutput *stillImageOutput; 166 } 167 168 @property (nonatomic, assign) id<CvPhotoCameraDelegate> delegate; 169 170 - (void)takePicture; 171 172 @end 173 174 //! @} videoio_ios 175