-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCanonCameraWrapper.h
More file actions
executable file
·231 lines (171 loc) · 7.9 KB
/
CanonCameraWrapper.h
File metadata and controls
executable file
·231 lines (171 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#pragma once
//Written by Theo Watson - theo@openframeworks.cc
//NOTE
//We are missing code for legacy devices
//We are missing some mac specific snippets
//Both should be easy to integrate
//You also need the Canon SDK which you can request from them or possibily find by other means floating around in the ether.
#include "EDSDK.h"
#include "EDSDKErrors.h"
#include "EDSDKTypes.h"
#define HAVE_OF
#ifdef HAVE_OF
#include "ofImage.h"
#endif
#ifndef BYTE
#define BYTE unsigned char
#endif
typedef enum{
CAMERA_UNKNOWN,
CAMERA_READY,
CAMERA_OPEN,
CAMERA_CLOSED,
}cameraState;
static int sdkRef = 0;
static void easyRelease(EdsBaseRef &ref){
if(ref != NULL){
EdsRelease(ref);
ref = NULL;
}
}
class memoryImage : public ofImage{
public:
bool loadFromMemory(int bytesToRead, unsigned char * jpegBytes, int rotateMode = 0){
FIMEMORY *hmem = NULL;
hmem = FreeImage_OpenMemory((BYTE *)jpegBytes, bytesToRead);
if (hmem == NULL){
printf("couldn't create memory handle! \n");
return false;
}
//get the file type!
FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem);
if( fif == -1 ){
printf("unable to guess format", fif);
return false;
FreeImage_CloseMemory(hmem);
}
//make the image!!
FIBITMAP * tmpBmp = FreeImage_LoadFromMemory(fif, hmem, 0);
if( rotateMode > 0 && rotateMode < 4){
FIBITMAP * oldBmp = tmpBmp;
if( rotateMode == 1)tmpBmp = FreeImage_RotateClassic(tmpBmp, 90);
if( rotateMode == 2)tmpBmp = FreeImage_RotateClassic(tmpBmp, 180);
if( rotateMode == 3)tmpBmp = FreeImage_RotateClassic(tmpBmp, 270);
FreeImage_Unload(oldBmp);
}
//FreeImage_FlipVertical(tmpBmp);
putBmpIntoPixels(tmpBmp, myPixels);
width = FreeImage_GetWidth(tmpBmp);
height = FreeImage_GetHeight(tmpBmp);
bpp = FreeImage_GetBPP(tmpBmp);
swapRgb(myPixels);
FreeImage_Unload(tmpBmp);
FreeImage_CloseMemory(hmem);
return true;
}
//shouldn't have to redefine this but a gcc bug means we do
inline void swapRgb(ofPixels &pix){
if (pix.bitsPerPixel != 8){
int sizePixels = pix.width*pix.height;
int cnt = 0;
unsigned char temp;
int byteCount = pix.bitsPerPixel/8;
while (cnt < sizePixels){
temp = pix.pixels[cnt*byteCount];
pix.pixels[cnt*byteCount] = pix.pixels[cnt*byteCount+2];
pix.pixels[cnt*byteCount+2] = temp;
cnt++;
}
}
}
};
//NOTE
//We are missing code for legacy devices
//We are missing some mac specific snippets
//Both should be easy to integrate
class CanonCameraWrapper{
public:
//---------------------------------------------------------------------
CanonCameraWrapper();
~CanonCameraWrapper();
//---------------------------------------------------------------------
// SDK AND SESSION MANAGEMENT
//---------------------------------------------------------------------
bool setup(int cameraID); //You must call this to init the canon sdk
void destroy(); //To clean up - also called by destructor
bool openSession(); //Begins communication with camera
bool closeSession(); //Ends communication with camera.
//Note on sessions: Commands like takePicture
//will open a session if none exists. This
//is slower though so consider calling it
//once at the begining of your app.
//---------------------------------------------------------------------
// CONFIG
//---------------------------------------------------------------------
void setDeleteFromCameraAfterDownload(bool deleteAfter);
void setDownloadPath(string downloadPathStr);
void enableDownloadOnTrigger(); //Trigger meaning takePicture
void disableDownloadOnTrigger(); //Trigger meaning takePicture
//---------------------------------------------------------------------
// ACTIONS
//---------------------------------------------------------------------
bool takePicture(); //Takes a picture. If enabled it will also download
//the image to the folder set by the download path.
bool sendCommand( EdsCameraCommand inCommand, EdsInt32 inParam = 0);
//---------------------------------------------------------------------
// LIVE VIEW
//---------------------------------------------------------------------
bool beginLiveView(); //starts live view
bool endLiveView(); //ends live view
bool grabPixelsFromLiveView(int rotateByNTimes90 = 0); //capture the live view to rgb pixel array
bool saveImageFromLiveView(string saveName);
bool getLiveViewActive(); //true if live view is enabled
int getLiveViewFrameNo(); //returns the number of live view frames passed
void resetLiveViewFrameCount(); //resets to 0
bool isLiveViewPixels(); //true if there is captured pixels
int getLiveViewPixelWidth(); //width of live view pixel data
int getLiveViewPixelHeight(); //height of live view pixel data
unsigned char * getLiveViewPixels(); //returns captured pixels
//---------------------------------------------------------------------
// MISC EXTRA STUFF
//---------------------------------------------------------------------
string getLastImageName(); //The full path of the last downloaded image
string getLastImagePath(); //The name of the last downloaded image
//This doesn't work perfectly - for some reason it can be one image behind
//something about how often the camera updates the SDK.
//Having the on picture event registered seems to help.
//But downloading via event is much more reliable at the moment.
//WARNING - If you are not taking pictures and you have bDeleteAfterDownload set to true
//you will be deleting the files that are on the camera.
//Simplified: be careful about calling this when you haven't just taken a photo.
bool downloadLastImage();
//Hmm - might be needed for threading - currently doesn't work
bool isTransfering();
protected:
//---------------------------------------------------------------------
// PROTECTED STUFF
//---------------------------------------------------------------------
bool downloadImage(EdsDirectoryItemRef directoryItem);
static EdsError EDSCALLBACK handleObjectEvent(EdsObjectEvent inEvent, EdsBaseRef object, EdsVoid *inContext);
static EdsError EDSCALLBACK handlePropertyEvent(EdsPropertyEvent inEvent, EdsPropertyID inPropertyID, EdsUInt32 inParam, EdsVoid * inContext);
static EdsError EDSCALLBACK handleStateEvent(EdsStateEvent inEvent, EdsUInt32 inEventData, EdsVoid * inContext);
void registerCallback();
bool preCommand();
void postCommand();
int livePixelsWidth;
int livePixelsHeight;
unsigned char * livePixels;
EdsUInt32 evfMode;
EdsUInt32 device;
int liveViewCurrentFrame;
string lastImageName;
string lastImagePath;
string downloadPath;
bool downloadEnabled;
bool bDeleteAfterDownload;
bool registered;
bool needToOpen;
cameraState state;
EdsCameraRef theCamera ;
EdsCameraListRef theCameraList;
};