1
+ #include " ofxEdsdk.h"
2
+
3
+ namespace ofxEdsdk {
4
+
5
+ void handleError (EdsError err, string msg) {
6
+ if (err != EDS_ERR_OK) {
7
+ cout << msg << " returned " << getEdsErrorString (err) << endl;
8
+ }
9
+ }
10
+
11
+ /*
12
+ After calling startLiveView, we get:
13
+ property event: kEdsPropID_Evf_OutputDevice / 0
14
+ property event: kEdsPropID_Evf_DepthOfFieldPreview / 0
15
+ property event: kEdsPropID_MeteringMode / 0
16
+ property event: kEdsPropID_FocusInfo / 0
17
+ property event: kEdsPropID_AFMode / 0
18
+ property event: kEdsPropID_ExposureCompensation / 0
19
+ property event: kEdsPropID_AFMode / 0
20
+ property event: kEdsPropID_MeteringMode / 0
21
+ */
22
+ EdsError EDSCALLBACK handlePropertyEvent (EdsPropertyEvent event, EdsPropertyID propertyId, EdsUInt32 param, EdsVoid* context) {
23
+ cout << " property event: " << getEdsPropertyString (propertyId) << " / " << param << endl;
24
+
25
+ if (propertyId == kEdsPropID_Evf_OutputDevice ) {
26
+ ((Camera*) context)->setLiveViewReady (true );
27
+ }
28
+ }
29
+
30
+ EdsError startLiveview (EdsCameraRef camera) {
31
+ EdsError err = EDS_ERR_OK;
32
+
33
+ // Get the output device for the live view image
34
+ EdsUInt32 device;
35
+ err = EdsGetPropertyData (camera, kEdsPropID_Evf_OutputDevice , 0 , sizeof (device), &device);
36
+
37
+ // PC live view starts by setting the PC as the output device for the live view image.
38
+ if (err == EDS_ERR_OK) {
39
+ device |= kEdsEvfOutputDevice_PC ;
40
+ err = EdsSetPropertyData (camera, kEdsPropID_Evf_OutputDevice , 0 , sizeof (device), &device);
41
+ }
42
+
43
+ // A property change event notification is issued from the camera if property settings are made successfully.
44
+ // Start downloading of the live view image once the property change notification arrives.
45
+ }
46
+
47
+ EdsError downloadEvfData (EdsCameraRef camera, ofPixels& pixels) {
48
+ EdsError err = EDS_ERR_OK;
49
+ EdsStreamRef stream = NULL ;
50
+ EdsEvfImageRef evfImage = NULL ;
51
+
52
+ // Create memory stream.
53
+ // This automatically allocates the stream if it's unallocated.
54
+ // If you want to save some time, avoid reallocation by keeping the EdsStreamRef around.
55
+ // Alternatively, you can prepare the memory yourself and use EdsCreateMemoryStreamFromPointer.
56
+ if (err == EDS_ERR_OK) {
57
+ err = EdsCreateMemoryStream (0 , &stream);
58
+ handleError (err, " EdsCreateMemoryStream" );
59
+ }
60
+
61
+ // Create EvfImageRef.
62
+ if (err == EDS_ERR_OK) {
63
+ err = EdsCreateEvfImageRef (stream, &evfImage);
64
+ handleError (err, " EdsCreateEvfImageRef" );
65
+ }
66
+
67
+ // Download live view image data.
68
+ if (err == EDS_ERR_OK) {
69
+ err = EdsDownloadEvfImage (camera, evfImage);
70
+ handleError (err, " EdsDownloadEvfImage" );
71
+ }
72
+
73
+ // Get the image data.
74
+ EdsUInt32 length;
75
+ if (err == EDS_ERR_OK) {
76
+ EdsGetLength (stream, &length);
77
+ handleError (err, " EdsGetLength" );
78
+ }
79
+
80
+ char * streamPointer;
81
+ if (err == EDS_ERR_OK) {
82
+ EdsGetPointer (stream, (EdsVoid**) &streamPointer);
83
+ handleError (err, " EdsGetPointer" );
84
+ }
85
+
86
+ if (err == EDS_ERR_OK) {
87
+ cout << " Copying image (" << length << " ) to ofBuffer" << endl;
88
+ ofBuffer imageBuffer;
89
+ imageBuffer.set (streamPointer, length);
90
+
91
+ ofLoadImage (pixels, imageBuffer);
92
+ }
93
+
94
+ /*
95
+ // Get the metadata of the image.
96
+ // Get the zoom ratio
97
+ EdsUInt32 zoom;
98
+ EdsGetPropertyData(evfImage, kEdsPropID_Evf_ZoomPosition, 0 , sizeof(zoom), &zoom);
99
+
100
+ // Get the focus and zoom border position
101
+ EdsPoint point;
102
+ EdsGetPropertyData(evfImage, kEdsPropID_Evf_ZoomPosition, 0 , sizeof(point), &point);
103
+ */
104
+
105
+ // Release stream
106
+ if (stream != NULL ) {
107
+ EdsRelease (stream);
108
+ handleError (err, " EdsRelease" );
109
+ stream = NULL ;
110
+ }
111
+
112
+ // Release evfImage
113
+ if (evfImage != NULL ) {
114
+ EdsRelease (evfImage);
115
+ handleError (err, " EdsRelease" );
116
+ evfImage = NULL ;
117
+ }
118
+
119
+ return err;
120
+ }
121
+
122
+ EdsError endLiveview (EdsCameraRef camera) {
123
+ EdsError err = EDS_ERR_OK;
124
+ // Get the output device for the live view image
125
+ EdsUInt32 device;
126
+ err = EdsGetPropertyData (camera, kEdsPropID_Evf_OutputDevice , 0 , sizeof (device), &device );
127
+ // PC live view ends if the PC is disconnected from the live view image output device.
128
+ if (err == EDS_ERR_OK) {
129
+ device &= ~kEdsEvfOutputDevice_PC ;
130
+ err = EdsSetPropertyData (camera, kEdsPropID_Evf_OutputDevice , 0 , sizeof (device), &device);
131
+ }
132
+ }
133
+
134
+ Camera::Camera () :
135
+ liveViewReady (false ),
136
+ frameNew (false ) {
137
+ }
138
+
139
+ Camera::~Camera () {
140
+ if (liveViewReady) {
141
+ endLiveview (camera);
142
+ }
143
+
144
+ err = EdsCloseSession (camera);
145
+ handleError (err, " EdsCloseSession" );
146
+
147
+ err = EdsTerminateSDK ();
148
+ handleError (err, " EdsTerminateSDK" );
149
+ }
150
+
151
+ void Camera::setup () {
152
+ cout << " EdsInitializeSDK()" << endl;
153
+ err = EdsInitializeSDK ();
154
+ handleError (err, " EdsInitializeSDK" );
155
+
156
+ EdsCameraListRef cameraList;
157
+ err = EdsGetCameraList (&cameraList);
158
+
159
+ EdsUInt32 cameraCount;
160
+ cout << " EdsGetChildCount()" << endl;
161
+ err = EdsGetChildCount (cameraList, &cameraCount);
162
+ handleError (err, " EdsGetChildCount" );
163
+
164
+ cout << " Camera count: " << cameraCount << endl;
165
+
166
+ EdsInt32 cameraIndex = 0 ;
167
+ cout << " EdsGetChildAtIndex()" << endl;
168
+ err = EdsGetChildAtIndex (cameraList, cameraIndex, &camera);
169
+ handleError (err, " EdsGetChildAtIndex" );
170
+
171
+ cout << " EdsSetPropertyEventHandler()" << endl;
172
+ err = EdsSetPropertyEventHandler (camera, kEdsPropertyEvent_All , handlePropertyEvent, this );
173
+ handleError (err, " EdsSetPropertyEventHandler" );
174
+
175
+ cout << " EdsOpenSession()" << endl;
176
+ err = EdsOpenSession (camera);
177
+ handleError (err, " EdsOpenSession" );
178
+
179
+ cout << " starting live view" << endl;
180
+ startLiveview (camera);
181
+ }
182
+
183
+ void Camera::update () {
184
+ if (liveViewReady) {
185
+ cout << " downloadEvfData()" << endl;
186
+ err = downloadEvfData (camera, livePixels);
187
+ if (err == EDS_ERR_OK) {
188
+ frameNew = true ;
189
+ if (liveTexture.getWidth () != livePixels.getWidth () ||
190
+ liveTexture.getHeight () != livePixels.getHeight ()) {
191
+ liveTexture.allocate (livePixels.getWidth (), livePixels.getHeight (), GL_RGB8);
192
+ }
193
+ liveTexture.loadData (livePixels);
194
+ }
195
+ }
196
+ }
197
+
198
+ bool Camera::isFrameNew () {
199
+ if (frameNew) {
200
+ frameNew = false ;
201
+ return true ;
202
+ } else {
203
+ return false ;
204
+ }
205
+ }
206
+
207
+ const ofPixels& Camera::getPixelsRef () const {
208
+ return livePixels;
209
+ }
210
+
211
+ ofPixels& Camera::getPixelsRef () {
212
+ return livePixels;
213
+ }
214
+
215
+ unsigned int Camera::getWidth () const {
216
+ return livePixels.getWidth ();
217
+ }
218
+
219
+ unsigned int Camera::getHeight () const {
220
+ return livePixels.getHeight ();
221
+ }
222
+
223
+ void Camera::draw (float x, float y) {
224
+ draw (x, y, getWidth (), getHeight ());
225
+ }
226
+
227
+ void Camera::draw (float x, float y, float width, float height) {
228
+ if (liveViewReady) {
229
+ ofPushMatrix ();
230
+ ofTranslate (x, y);
231
+ liveTexture.draw (0 , 0 , width, height);
232
+ stringstream status;
233
+ status << livePixels.getWidth () << " x" << livePixels.getHeight () << " @ " << (int ) ofGetFrameRate () << " fps" ;
234
+ ofDrawBitmapString (status.str (), 10 , 20 );
235
+ ofPopMatrix ();
236
+ }
237
+ }
238
+
239
+ void Camera::setLiveViewReady (bool liveViewReady) {
240
+ this ->liveViewReady = liveViewReady;
241
+ }
242
+ }
0 commit comments