forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcap_gphoto2.cpp
1222 lines (1136 loc) · 35 KB
/
cap_gphoto2.cpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2015, Piotr Dobrowolski dobrypd[at]gmail[dot]com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "precomp.hpp"
#ifdef HAVE_GPHOTO2
#include <gphoto2/gphoto2.h>
#include <algorithm>
#include <clocale>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <map>
#include <ostream>
#include <string>
namespace cv
{
namespace gphoto2 {
/**
* \brief Map gPhoto2 return code into this exception.
*/
class GPhoto2Exception: public std::exception
{
private:
int result;
const char * method;
public:
/**
* @param methodStr libgphoto2 method name
* @param gPhoto2Result libgphoto2 method result, should be less than GP_OK
*/
GPhoto2Exception(const char * methodStr, int gPhoto2Result)
{
result = gPhoto2Result;
method = methodStr;
}
virtual const char * what() const throw() CV_OVERRIDE
{
return gp_result_as_string(result);
}
friend std::ostream & operator<<(std::ostream & ostream,
const GPhoto2Exception & e)
{
return ostream << e.method << ": " << e.what();
}
};
/**
* \brief Capture using your camera device via digital camera library - gPhoto2.
*
* For library description and list of supported cameras, go to
* @url http://gphoto.sourceforge.net/
*
* Because gPhoto2 configuration is based on a widgets
* and OpenCV CvCapture property settings are double typed
* some assumptions and tricks has to be made.
* 1. Device properties can be changed by IDs, use @method setProperty(int, double)
* and @method getProperty(int) with __additive inversed__
* camera setting ID as propertyId. (If you want to get camera setting
* with ID == x, you want to call #getProperty(-x)).
* 2. Digital camera settings IDs are device dependent.
* 3. You can list them by getting property CAP_PROP_GPHOTO2_WIDGET_ENUMERATE.
* 3.1. As return you will get pointer to char array (with listed properties)
* instead of double. This list is in CSV type.
* 4. There are several types of widgets (camera settings).
* 4.1. For "menu" and "radio", you can get/set choice number.
* 4.2. For "toggle" you can get/set int type.
* 4.3. For "range" you can get/set float.
* 4.4. For any other pointer will be fetched/set.
* 5. You can fetch camera messages by using CAP_PROP_GPHOTO2_COLLECT_MSGS
* and CAP_PROP_GPHOTO2_FLUSH_MSGS (will return pointer to char array).
* 6. Camera settings are fetched from device as lazy as possible.
* It creates problem with situation when change of one setting
* affects another setting. You can use CV_CAP_PROP_GPHOTO2_RELOAD_ON_CHANGE
* or CV_CAP_PROP_GPHOTO2_RELOAD_CONFIG to be sure that property you are
* planning to get will be actual.
*
* Capture can work in 2 main modes: preview and final.
* Where preview is an output from digital camera "liveview".
* Change modes with CAP_PROP_GPHOTO2_PREVIEW property.
*
* Moreover some generic properties are mapped to widgets, or implemented:
* * CV_CAP_PROP_SPEED,
* * CV_CAP_PROP_APERATURE,
* * CV_CAP_PROP_EXPOSUREPROGRAM,
* * CV_CAP_PROP_VIEWFINDER,
* * CV_CAP_PROP_POS_MSEC,
* * CV_CAP_PROP_POS_FRAMES,
* * CV_CAP_PROP_FRAME_WIDTH,
* * CV_CAP_PROP_FRAME_HEIGHT,
* * CV_CAP_PROP_FPS,
* * CV_CAP_PROP_FRAME_COUNT
* * CV_CAP_PROP_FORMAT,
* * CV_CAP_PROP_EXPOSURE,
* * CV_CAP_PROP_TRIGGER_DELAY,
* * CV_CAP_PROP_ZOOM,
* * CV_CAP_PROP_FOCUS,
* * CV_CAP_PROP_ISO_SPEED.
*/
class DigitalCameraCapture: public IVideoCapture
{
public:
static const char * separator;
static const char * lineDelimiter;
DigitalCameraCapture();
DigitalCameraCapture(int index);
DigitalCameraCapture(const String &deviceName);
virtual ~DigitalCameraCapture() CV_OVERRIDE;
virtual bool isOpened() const CV_OVERRIDE;
virtual double getProperty(int) const CV_OVERRIDE;
virtual bool setProperty(int, double) CV_OVERRIDE;
virtual bool grabFrame() CV_OVERRIDE;
virtual bool retrieveFrame(int, OutputArray) CV_OVERRIDE;
virtual int getCaptureDomain() CV_OVERRIDE { return CV_CAP_GPHOTO2; }
bool open(int index);
void close();
bool deviceExist(int index) const;
int findDevice(const char * deviceName) const;
protected:
// Known widget names
static const char * PROP_EXPOSURE_COMPENSACTION;
static const char * PROP_SELF_TIMER_DELAY;
static const char * PROP_MANUALFOCUS;
static const char * PROP_AUTOFOCUS;
static const char * PROP_ISO;
static const char * PROP_SPEED;
static const char * PROP_APERTURE_NIKON;
static const char * PROP_APERTURE_CANON;
static const char * PROP_EXPOSURE_PROGRAM;
static const char * PROP_VIEWFINDER;
// Instance
GPContext * context = NULL;
int numDevices;
void initContext();
// Selected device
bool opened;
Camera * camera = NULL;
Mat frame;
// Properties
CameraWidget * rootWidget = NULL;
CameraWidget * getGenericProperty(int propertyId, double & output) const;
CameraWidget * setGenericProperty(int propertyId, double value,
bool & output) const;
// Widgets
void reloadConfig();
CameraWidget * getWidget(int widgetId) const;
CameraWidget * findWidgetByName(const char * name) const;
// Loading
void readFrameFromFile(CameraFile * file, OutputArray outputFrame);
// Context feedback
friend void ctxErrorFunc(GPContext *, const char *, void *);
friend void ctxStatusFunc(GPContext *, const char *, void *);
friend void ctxMessageFunc(GPContext *, const char *, void *);
// Messages / debug
enum MsgType
{
ERROR = (int) 'E',
WARNING = (int) 'W',
STATUS = (int) 'S',
OTHER = (int) 'O'
};
template<typename OsstreamPrintable>
void message(MsgType msgType, const char * msg,
OsstreamPrintable & arg) const;
private:
// Instance
CameraAbilitiesList * abilitiesList = NULL;
GPPortInfoList * capablePorts = NULL;
CameraList * allDevices = NULL;
// Selected device
CameraAbilities cameraAbilities;
std::deque<CameraFile *> grabbedFrames;
// Properties
bool preview; // CV_CAP_PROP_GPHOTO2_PREVIEW
std::string widgetInfo; // CV_CAP_PROP_GPHOTO2_WIDGET_ENUMERATE
std::map<int, CameraWidget *> widgets;
bool reloadOnChange; // CV_CAP_PROP_GPHOTO2_RELOAD_ON_CHANGE
time_t firstCapturedFrameTime;
unsigned long int capturedFrames;
DigitalCameraCapture(const DigitalCameraCapture&); // Disable copying
DigitalCameraCapture& operator=(DigitalCameraCapture const&); // Disable assigning
// Widgets
int noOfWidgets;
int widgetDescription(std::ostream &os, CameraWidget * widget) const;
int collectWidgets(std::ostream &os, CameraWidget * widget);
// Messages / debug
mutable std::ostringstream msgsBuffer; // CV_CAP_PROP_GPHOTO2_FLUSH_MSGS
mutable std::string lastFlush; // CV_CAP_PROP_GPHOTO2_FLUSH_MSGS
bool collectMsgs; // CV_CAP_PROP_GPHOTO2_COLLECT_MSGS
};
/**
* \brief Check if gPhoto2 function ends successfully. If not, throw an exception.
*/
#define CR(GPHOTO2_FUN) do {\
int r_0629c47b758;\
if ((r_0629c47b758 = (GPHOTO2_FUN)) < GP_OK) {\
throw GPhoto2Exception(#GPHOTO2_FUN, r_0629c47b758);\
};\
} while(0)
/**
* \brief gPhoto2 context error feedback function.
* @param thatGPhotoCap is required to be pointer to DigitalCameraCapture object.
*/
void ctxErrorFunc(GPContext *, const char * str, void * thatGPhotoCap)
{
const DigitalCameraCapture * self =
(const DigitalCameraCapture *) thatGPhotoCap;
self->message(self->ERROR, "context feedback", str);
}
/**
* \brief gPhoto2 context status feedback function.
* @param thatGPhotoCap is required to be pointer to DigitalCameraCapture object.
*/
void ctxStatusFunc(GPContext *, const char * str, void * thatGPhotoCap)
{
const DigitalCameraCapture * self =
(const DigitalCameraCapture *) thatGPhotoCap;
self->message(self->STATUS, "context feedback", str);
}
/**
* \brief gPhoto2 context message feedback function.
* @param thatGPhotoCap is required to be pointer to DigitalCameraCapture object.
*/
void ctxMessageFunc(GPContext *, const char * str, void * thatGPhotoCap)
{
const DigitalCameraCapture * self =
(const DigitalCameraCapture *) thatGPhotoCap;
self->message(self->OTHER, "context feedback", str);
}
/**
* \brief Separator used while creating CSV.
*/
const char * DigitalCameraCapture::separator = ",";
/**
* \brief Line delimiter used while creating any readable output.
*/
const char * DigitalCameraCapture::lineDelimiter = "\n";
/**
* \bief Some known widget names.
*
* Those are actually substrings of widget name.
* ie. for VIEWFINDER, Nikon uses "viewfinder", while Canon can use "eosviewfinder".
*/
const char * DigitalCameraCapture::PROP_EXPOSURE_COMPENSACTION =
"exposurecompensation";
const char * DigitalCameraCapture::PROP_SELF_TIMER_DELAY = "selftimerdelay";
const char * DigitalCameraCapture::PROP_MANUALFOCUS = "manualfocusdrive";
const char * DigitalCameraCapture::PROP_AUTOFOCUS = "autofocusdrive";
const char * DigitalCameraCapture::PROP_ISO = "iso";
const char * DigitalCameraCapture::PROP_SPEED = "shutterspeed";
const char * DigitalCameraCapture::PROP_APERTURE_NIKON = "f-number";
const char * DigitalCameraCapture::PROP_APERTURE_CANON = "aperture";
const char * DigitalCameraCapture::PROP_EXPOSURE_PROGRAM = "expprogram";
const char * DigitalCameraCapture::PROP_VIEWFINDER = "viewfinder";
/**
* Initialize gPhoto2 context, search for all available devices.
*/
void DigitalCameraCapture::initContext()
{
capturedFrames = noOfWidgets = numDevices = 0;
opened = preview = reloadOnChange = false;
firstCapturedFrameTime = 0;
context = gp_context_new();
gp_context_set_error_func(context, ctxErrorFunc, (void*) this);
gp_context_set_status_func(context, ctxStatusFunc, (void*) this);
gp_context_set_message_func(context, ctxMessageFunc, (void*) this);
try
{
// Load abilities
CR(gp_abilities_list_new(&abilitiesList));
CR(gp_abilities_list_load(abilitiesList, context));
// Load ports
CR(gp_port_info_list_new(&capablePorts));
CR(gp_port_info_list_load(capablePorts));
// Auto-detect devices
CR(gp_list_new(&allDevices));
CR(gp_camera_autodetect(allDevices, context));
CR(numDevices = gp_list_count(allDevices));
}
catch (const GPhoto2Exception & e)
{
numDevices = 0;
}
}
/**
* Search for all devices while constructing.
*/
DigitalCameraCapture::DigitalCameraCapture()
{
initContext();
}
/**
* @see open(int)
*/
DigitalCameraCapture::DigitalCameraCapture(int index)
{
initContext();
if (deviceExist(index))
open(index);
}
/**
* @see findDevice(const char*)
* @see open(int)
*/
DigitalCameraCapture::DigitalCameraCapture(const String & deviceName)
{
initContext();
int index = findDevice(deviceName.c_str());
if (deviceExist(index))
open(index);
}
/**
* Always close connection to the device.
*/
DigitalCameraCapture::~DigitalCameraCapture()
{
close();
try
{
CR(gp_abilities_list_free(abilitiesList));
abilitiesList = NULL;
CR(gp_port_info_list_free(capablePorts));
capablePorts = NULL;
CR(gp_list_unref(allDevices));
allDevices = NULL;
gp_context_unref(context);
context = NULL;
}
catch (const GPhoto2Exception & e)
{
message(ERROR, "destruction error", e);
}
}
/**
* Connects to selected device.
*/
bool DigitalCameraCapture::open(int index)
{
const char * model = 0, *path = 0;
int m, p;
GPPortInfo portInfo;
if (isOpened()) {
close();
}
try
{
CR(gp_camera_new(&camera));
CR(gp_list_get_name(allDevices, index, &model));
CR(gp_list_get_value(allDevices, index, &path));
// Set model abilities.
CR(m = gp_abilities_list_lookup_model(abilitiesList, model));
CR(gp_abilities_list_get_abilities(abilitiesList, m, &cameraAbilities));
CR(gp_camera_set_abilities(camera, cameraAbilities));
// Set port
CR(p = gp_port_info_list_lookup_path(capablePorts, path));
CR(gp_port_info_list_get_info(capablePorts, p, &portInfo));
CR(gp_camera_set_port_info(camera, portInfo));
// Initialize connection to the camera.
CR(gp_camera_init(camera, context));
message(STATUS, "connected camera", model);
message(STATUS, "connected using", path);
// State initialization
firstCapturedFrameTime = 0;
capturedFrames = 0;
preview = false;
reloadOnChange = false;
collectMsgs = false;
reloadConfig();
opened = true;
return true;
}
catch (const GPhoto2Exception & e)
{
message(WARNING, "opening device failed", e);
return false;
}
}
/**
*
*/
bool DigitalCameraCapture::isOpened() const
{
return opened;
}
/**
* Close connection to the camera. Remove all unread frames/files.
*/
void DigitalCameraCapture::close()
{
try
{
if (!frame.empty())
{
frame.release();
}
if (camera)
{
CR(gp_camera_exit(camera, context));
CR(gp_camera_unref(camera));
camera = NULL;
}
opened = false;
if (int frames = grabbedFrames.size() > 0)
{
while (frames--)
{
CameraFile * file = grabbedFrames.front();
grabbedFrames.pop_front();
CR(gp_file_unref(file));
}
}
if (rootWidget)
{
widgetInfo.clear();
CR(gp_widget_unref(rootWidget));
rootWidget = NULL;
}
}
catch (const GPhoto2Exception & e)
{
message(ERROR, "cannot close device properly", e);
}
}
/**
* @param output will be changed if possible, return 0 if changed,
* @return widget, or NULL if output value was found (saved in argument),
*/
CameraWidget * DigitalCameraCapture::getGenericProperty(int propertyId,
double & output) const
{
switch (propertyId)
{
case CV_CAP_PROP_POS_MSEC:
{
// Only seconds level precision, FUTURE: cross-platform milliseconds
output = (time(0) - firstCapturedFrameTime) * 1e2;
return NULL;
}
case CV_CAP_PROP_POS_FRAMES:
{
output = capturedFrames;
return NULL;
}
case CV_CAP_PROP_FRAME_WIDTH:
{
if (!frame.empty())
{
output = frame.cols;
}
return NULL;
}
case CV_CAP_PROP_FRAME_HEIGHT:
{
if (!frame.empty())
{
output = frame.rows;
}
return NULL;
}
case CV_CAP_PROP_FORMAT:
{
if (!frame.empty())
{
output = frame.type();
}
return NULL;
}
case CV_CAP_PROP_FPS: // returns average fps from the begin
{
double wholeProcessTime = 0;
getGenericProperty(CV_CAP_PROP_POS_MSEC, wholeProcessTime);
wholeProcessTime /= 1e2;
output = capturedFrames / wholeProcessTime;
return NULL;
}
case CV_CAP_PROP_FRAME_COUNT:
{
output = capturedFrames;
return NULL;
}
case CV_CAP_PROP_EXPOSURE:
return findWidgetByName(PROP_EXPOSURE_COMPENSACTION);
case CV_CAP_PROP_TRIGGER_DELAY:
return findWidgetByName(PROP_SELF_TIMER_DELAY);
case CV_CAP_PROP_ZOOM:
return findWidgetByName(PROP_MANUALFOCUS);
case CV_CAP_PROP_FOCUS:
return findWidgetByName(PROP_AUTOFOCUS);
case CV_CAP_PROP_ISO_SPEED:
return findWidgetByName(PROP_ISO);
case CV_CAP_PROP_SPEED:
return findWidgetByName(PROP_SPEED);
case CV_CAP_PROP_APERTURE:
{
CameraWidget * widget = findWidgetByName(PROP_APERTURE_NIKON);
return (widget == 0) ? findWidgetByName(PROP_APERTURE_CANON) : widget;
}
case CV_CAP_PROP_EXPOSUREPROGRAM:
return findWidgetByName(PROP_EXPOSURE_PROGRAM);
case CV_CAP_PROP_VIEWFINDER:
return findWidgetByName(PROP_VIEWFINDER);
}
return NULL;
}
/**
* Get property.
* @see DigitalCameraCapture for more information about returned double type.
*/
double DigitalCameraCapture::getProperty(int propertyId) const
{
CameraWidget * widget = NULL;
double output = 0;
if (propertyId < 0)
{
widget = getWidget(-propertyId);
}
else
{
switch (propertyId)
{
// gphoto2 cap featured
case CV_CAP_PROP_GPHOTO2_PREVIEW:
return preview;
case CV_CAP_PROP_GPHOTO2_WIDGET_ENUMERATE:
if (rootWidget == NULL)
return 0;
return (intptr_t) widgetInfo.c_str();
case CV_CAP_PROP_GPHOTO2_RELOAD_CONFIG:
return 0; // Trigger, only by set
case CV_CAP_PROP_GPHOTO2_RELOAD_ON_CHANGE:
return reloadOnChange;
case CV_CAP_PROP_GPHOTO2_COLLECT_MSGS:
return collectMsgs;
case CV_CAP_PROP_GPHOTO2_FLUSH_MSGS:
lastFlush = msgsBuffer.str();
msgsBuffer.str("");
msgsBuffer.clear();
return (intptr_t) lastFlush.c_str();
default:
widget = getGenericProperty(propertyId, output);
/* no break */
}
}
if (widget == NULL)
return output;
try
{
CameraWidgetType type;
CR(gp_widget_get_type(widget, &type));
switch (type)
{
case GP_WIDGET_MENU:
case GP_WIDGET_RADIO:
{
int cnt = 0, i;
const char * current;
CR(gp_widget_get_value(widget, ¤t));
CR(cnt = gp_widget_count_choices(widget));
for (i = 0; i < cnt; i++)
{
const char *choice;
CR(gp_widget_get_choice(widget, i, &choice));
if (std::strcmp(choice, current) == 0)
{
return i;
}
}
return -1;
}
case GP_WIDGET_TOGGLE:
{
int value;
CR(gp_widget_get_value(widget, &value));
return value;
}
case GP_WIDGET_RANGE:
{
float value;
CR(gp_widget_get_value(widget, &value));
return value;
}
default:
{
char* value;
CR(gp_widget_get_value(widget, &value));
return (intptr_t) value;
}
}
}
catch (const GPhoto2Exception & e)
{
char buf[128] = "";
sprintf(buf, "cannot get property: %d", propertyId);
message(WARNING, (const char *) buf, e);
return 0;
}
}
/**
* @param output will be changed if possible, return 0 if changed,
* @return widget, or 0 if output value was found (saved in argument),
*/
CameraWidget * DigitalCameraCapture::setGenericProperty(int propertyId,
double /*FUTURE: value*/, bool & output) const
{
switch (propertyId)
{
case CV_CAP_PROP_POS_MSEC:
case CV_CAP_PROP_POS_FRAMES:
case CV_CAP_PROP_FRAME_WIDTH:
case CV_CAP_PROP_FRAME_HEIGHT:
case CV_CAP_PROP_FPS:
case CV_CAP_PROP_FRAME_COUNT:
case CV_CAP_PROP_FORMAT:
output = false;
return NULL;
case CV_CAP_PROP_EXPOSURE:
return findWidgetByName(PROP_EXPOSURE_COMPENSACTION);
case CV_CAP_PROP_TRIGGER_DELAY:
return findWidgetByName(PROP_SELF_TIMER_DELAY);
case CV_CAP_PROP_ZOOM:
return findWidgetByName(PROP_MANUALFOCUS);
case CV_CAP_PROP_FOCUS:
return findWidgetByName(PROP_AUTOFOCUS);
case CV_CAP_PROP_ISO_SPEED:
return findWidgetByName(PROP_ISO);
case CV_CAP_PROP_SPEED:
return findWidgetByName(PROP_SPEED);
case CV_CAP_PROP_APERTURE:
{
CameraWidget * widget = findWidgetByName(PROP_APERTURE_NIKON);
return (widget == NULL) ? findWidgetByName(PROP_APERTURE_CANON) : widget;
}
case CV_CAP_PROP_EXPOSUREPROGRAM:
return findWidgetByName(PROP_EXPOSURE_PROGRAM);
case CV_CAP_PROP_VIEWFINDER:
return findWidgetByName(PROP_VIEWFINDER);
}
return NULL;
}
/**
* Set property.
* @see DigitalCameraCapture for more information about value, double typed, argument.
*/
bool DigitalCameraCapture::setProperty(int propertyId, double value)
{
CameraWidget * widget = NULL;
bool output = false;
if (propertyId < 0)
{
widget = getWidget(-propertyId);
}
else
{
switch (propertyId)
{
// gphoto2 cap featured
case CV_CAP_PROP_GPHOTO2_PREVIEW:
preview = value != 0;
return true;
case CV_CAP_PROP_GPHOTO2_WIDGET_ENUMERATE:
return false;
case CV_CAP_PROP_GPHOTO2_RELOAD_CONFIG:
reloadConfig();
return true;
case CV_CAP_PROP_GPHOTO2_RELOAD_ON_CHANGE:
reloadOnChange = value != 0;
return true;
case CV_CAP_PROP_GPHOTO2_COLLECT_MSGS:
collectMsgs = value != 0;
return true;
case CV_CAP_PROP_GPHOTO2_FLUSH_MSGS:
return false;
default:
widget = setGenericProperty(propertyId, value, output);
/* no break */
}
}
if (widget == NULL)
return output;
try
{
CameraWidgetType type;
CR(gp_widget_get_type(widget, &type));
switch (type)
{
case GP_WIDGET_RADIO:
case GP_WIDGET_MENU:
{
int i = static_cast<int>(value);
char *choice;
CR(gp_widget_get_choice(widget, i, (const char**)&choice));
CR(gp_widget_set_value(widget, choice));
break;
}
case GP_WIDGET_TOGGLE:
{
int i = static_cast<int>(value);
CR(gp_widget_set_value(widget, &i));
break;
}
case GP_WIDGET_RANGE:
{
float v = static_cast<float>(value);
CR(gp_widget_set_value(widget, &v));
break;
}
default:
{
CR(gp_widget_set_value(widget, (void* )(intptr_t )&value));
break;
}
}
if (!reloadOnChange)
{
// force widget change
CR(gp_widget_set_changed(widget, 1));
}
// Use the same locale setting as while getting rootWidget.
char * localeTmp = setlocale(LC_ALL, "C");
CR(gp_camera_set_config(camera, rootWidget, context));
setlocale(LC_ALL, localeTmp);
if (reloadOnChange)
{
reloadConfig();
} else {
CR(gp_widget_set_changed(widget, 0));
}
}
catch (const GPhoto2Exception & e)
{
char buf[128] = "";
sprintf(buf, "cannot set property: %d to %f", propertyId, value);
message(WARNING, (const char *) buf, e);
return false;
}
return true;
}
/**
* Capture image, and store file in @field grabbedFrames.
* Do not read a file. File will be deleted from camera automatically.
*/
bool DigitalCameraCapture::grabFrame()
{
CameraFilePath filePath;
CameraFile * file = NULL;
try
{
CR(gp_file_new(&file));
if (preview)
{
CR(gp_camera_capture_preview(camera, file, context));
}
else
{
// Capture an image
CR(gp_camera_capture(camera, GP_CAPTURE_IMAGE, &filePath, context));
CR(gp_camera_file_get(camera, filePath.folder, filePath.name, GP_FILE_TYPE_NORMAL,
file, context));
CR(gp_camera_file_delete(camera, filePath.folder, filePath.name, context));
}
// State update
if (firstCapturedFrameTime == 0)
{
firstCapturedFrameTime = time(0);
}
capturedFrames++;
grabbedFrames.push_back(file);
}
catch (const GPhoto2Exception & e)
{
if (file)
gp_file_unref(file);
message(WARNING, "cannot grab new frame", e);
return false;
}
return true;
}
/**
* Read stored file with image.
*/
bool DigitalCameraCapture::retrieveFrame(int, OutputArray outputFrame)
{
if (grabbedFrames.size() > 0)
{
CameraFile * file = grabbedFrames.front();
grabbedFrames.pop_front();
try
{
readFrameFromFile(file, outputFrame);
CR(gp_file_unref(file));
}
catch (const GPhoto2Exception & e)
{
message(WARNING, "cannot read file grabbed from device", e);
return false;
}
}
else
{
return false;
}
return true;
}
/**
* @return true if device exists
*/
bool DigitalCameraCapture::deviceExist(int index) const
{
return (numDevices > 0) && (index < numDevices);
}
/**
* @return device index if exists, otherwise -1
*/
int DigitalCameraCapture::findDevice(const char * deviceName) const
{
const char * model = 0;
try
{
if (deviceName != 0)
{
for (int i = 0; i < numDevices; ++i)
{
CR(gp_list_get_name(allDevices, i, &model));
if (model != 0 && strstr(model, deviceName))
{
return i;
}
}
}
}
catch (const GPhoto2Exception & e)
{
; // pass
}
return -1;
}
/**
* Load device settings.
*/
void DigitalCameraCapture::reloadConfig()
{
std::ostringstream widgetInfoListStream;
if (rootWidget != NULL)
{
widgetInfo.clear();
CR(gp_widget_unref(rootWidget));
rootWidget = NULL;
widgets.clear();
}
// Make sure, that all configs (getting setting) will use the same locale setting.
char * localeTmp = setlocale(LC_ALL, "C");
CR(gp_camera_get_config(camera, &rootWidget, context));
setlocale(LC_ALL, localeTmp);
widgetInfoListStream << "id,label,name,info,readonly,type,value,"
<< lineDelimiter;
noOfWidgets = collectWidgets(widgetInfoListStream, rootWidget) + 1;
widgetInfo = widgetInfoListStream.str();
}
/**
* Get widget which was fetched in time of last call to @reloadConfig().
*/
CameraWidget * DigitalCameraCapture::getWidget(int widgetId) const
{
CameraWidget * widget;
std::map<int, CameraWidget *>::const_iterator it = widgets.find(widgetId);
if (it == widgets.end())
return 0;
widget = it->second;
return widget;
}
/**
* Search for widget with name which has @param subName substring.
*/
CameraWidget * DigitalCameraCapture::findWidgetByName(
const char * subName) const
{
if (subName != NULL)
{
try
{
const char * name;
typedef std::map<int, CameraWidget *>::const_iterator it_t;
it_t it = widgets.begin(), end = widgets.end();
while (it != end)
{
CR(gp_widget_get_name(it->second, &name));
if (strstr(name, subName))
break;
++it;
}
return (it != end) ? it->second : NULL;
}
catch (const GPhoto2Exception & e)
{
message(WARNING, "error while searching for widget", e);
}
}
return 0;
}
/**
* Image file reader.
*
* @FUTURE: RAW format reader.
*/
void DigitalCameraCapture::readFrameFromFile(CameraFile * file, OutputArray outputFrame)
{
// FUTURE: OpenCV cannot read RAW files right now.
const char * data;