1 /* 2 * cap_ios_photo_camera.mm 3 * For iOS video I/O 4 * by Eduard Feicho on 29/07/12 5 * Copyright 2012. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * 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. 17 * 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. 28 * 29 */ 30 31 32 #import "opencv2/videoio/cap_ios.h" 33 #include "precomp.hpp" 34 35 #pragma mark - Private Interface 36 37 38 @interface CvPhotoCamera () 39 40 @property (nonatomic, retain) AVCaptureStillImageOutput* stillImageOutput; 41 42 @end 43 44 45 46 #pragma mark - Implementation 47 48 49 @implementation CvPhotoCamera 50 51 52 53 #pragma mark Public 54 55 @synthesize stillImageOutput; 56 @synthesize delegate; 57 58 59 #pragma mark - Public interface 60 61 62 - (void)takePicture 63 { 64 if (cameraAvailable == NO) { 65 return; 66 } 67 cameraAvailable = NO; 68 69 70 [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:self.videoCaptureConnection 71 completionHandler: 72 ^(CMSampleBufferRef imageSampleBuffer, NSError *error) 73 { 74 if (error == nil && imageSampleBuffer != NULL) 75 { 76 // TODO check 77 // NSNumber* imageOrientation = [UIImage cgImageOrientationForUIDeviceOrientation:currentDeviceOrientation]; 78 // CMSetAttachment(imageSampleBuffer, kCGImagePropertyOrientation, imageOrientation, 1); 79 80 NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 81 82 dispatch_async(dispatch_get_main_queue(), ^{ 83 [self.captureSession stopRunning]; 84 85 // Make sure we create objects on the main thread in the main context 86 UIImage* newImage = [UIImage imageWithData:jpegData]; 87 88 //UIImageOrientation orientation = [newImage imageOrientation]; 89 90 // TODO: only apply rotation, don't scale, since we can set this directly in the camera 91 /* 92 switch (orientation) { 93 case UIImageOrientationUp: 94 case UIImageOrientationDown: 95 newImage = [newImage imageWithAppliedRotationAndMaxSize:CGSizeMake(640.0, 480.0)]; 96 break; 97 case UIImageOrientationLeft: 98 case UIImageOrientationRight: 99 newImage = [newImage imageWithMaxSize:CGSizeMake(640.0, 480.0)]; 100 default: 101 break; 102 } 103 */ 104 105 // We have captured the image, we can allow the user to take another picture 106 cameraAvailable = YES; 107 108 NSLog(@"CvPhotoCamera captured image"); 109 if (self.delegate) { 110 [self.delegate photoCamera:self capturedImage:newImage]; 111 } 112 113 [self.captureSession startRunning]; 114 }); 115 } 116 }]; 117 118 119 } 120 121 - (void)stop; 122 { 123 [super stop]; 124 self.stillImageOutput = nil; 125 } 126 127 128 #pragma mark - Private Interface 129 130 131 - (void)createStillImageOutput; 132 { 133 // setup still image output with jpeg codec 134 self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; 135 NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil]; 136 [self.stillImageOutput setOutputSettings:outputSettings]; 137 [self.captureSession addOutput:self.stillImageOutput]; 138 139 for (AVCaptureConnection *connection in self.stillImageOutput.connections) { 140 for (AVCaptureInputPort *port in [connection inputPorts]) { 141 if ([port.mediaType isEqual:AVMediaTypeVideo]) { 142 self.videoCaptureConnection = connection; 143 break; 144 } 145 } 146 if (self.videoCaptureConnection) { 147 break; 148 } 149 } 150 NSLog(@"[Camera] still image output created"); 151 } 152 153 154 - (void)createCaptureOutput; 155 { 156 [self createStillImageOutput]; 157 } 158 159 - (void)createCustomVideoPreview; 160 { 161 //do nothing, always use AVCaptureVideoPreviewLayer 162 } 163 164 165 @end 166