2 * cap_ios_photo_camera.mm
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.
32 #import "opencv2/highgui/cap_ios.h"
33 #include "precomp.hpp"
35 #pragma mark - Private Interface
38 @interface CvPhotoCamera ()
40 @property (nonatomic, retain) AVCaptureStillImageOutput* stillImageOutput;
46 #pragma mark - Implementation
49 @implementation CvPhotoCamera
55 @synthesize stillImageOutput;
59 #pragma mark - Public interface
64 if (cameraAvailable == NO) {
70 [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:self.videoCaptureConnection
72 ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
74 if (error == nil && imageSampleBuffer != NULL)
77 // NSNumber* imageOrientation = [UIImage cgImageOrientationForUIDeviceOrientation:currentDeviceOrientation];
78 // CMSetAttachment(imageSampleBuffer, kCGImagePropertyOrientation, imageOrientation, 1);
80 NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
82 dispatch_async(dispatch_get_main_queue(), ^{
83 [self.captureSession stopRunning];
85 // Make sure we create objects on the main thread in the main context
86 UIImage* newImage = [UIImage imageWithData:jpegData];
88 //UIImageOrientation orientation = [newImage imageOrientation];
90 // TODO: only apply rotation, don't scale, since we can set this directly in the camera
92 switch (orientation) {
93 case UIImageOrientationUp:
94 case UIImageOrientationDown:
95 newImage = [newImage imageWithAppliedRotationAndMaxSize:CGSizeMake(640.0, 480.0)];
97 case UIImageOrientationLeft:
98 case UIImageOrientationRight:
99 newImage = [newImage imageWithMaxSize:CGSizeMake(640.0, 480.0)];
105 // We have captured the image, we can allow the user to take another picture
106 cameraAvailable = YES;
108 NSLog(@"CvPhotoCamera captured image");
110 [self.delegate photoCamera:self capturedImage:newImage];
113 [self.captureSession startRunning];
124 self.stillImageOutput = nil;
128 #pragma mark - Private Interface
131 - (void)createStillImageOutput;
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];
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;
146 if (self.videoCaptureConnection) {
150 NSLog(@"[Camera] still image output created");
154 - (void)createCaptureOutput;
156 [self createStillImageOutput];
159 - (void)createCustomVideoPreview;
161 //do nothing, always use AVCaptureVideoPreviewLayer