1 /* compile with gcc -Wall -o canon-capture -lgphoto2 canon-capture.c
2 * This code released into the public domain 21 July 2008
4 * This program does the equivalent of:
6 * > set-config capture=1
7 * > capture-image-and-download
8 * compile with gcc -Wall -o canon-capture -lgphoto2 canon-capture.c
10 * Taken from: http://credentiality2.blogspot.com/2008/07/linux-libgphoto2-image-capture-from.html
15 #include <sys/types.h>
22 #include <gphoto2/gphoto2.h>
26 struct timeval starttime;
28 static void errordumper(GPLogLevel level, const char *domain, const char *format,
29 va_list args, void *data) {
32 gettimeofday(&tv,NULL);
33 tv.tv_sec -= starttime.tv_sec;
34 tv.tv_usec -= starttime.tv_usec;
36 tv.tv_usec += 1000000;
39 fprintf(stdout,"%d.%06d: ", (int)tv.tv_sec, (int)tv.tv_usec);
40 vfprintf(stdout, format, args);
41 fprintf(stdout, "\n");
45 /* This seems to have no effect on where images go
46 void set_capturetarget(Camera *canon, GPContext *canoncontext) {
48 printf("Get root config.\n");
49 CameraWidget *rootconfig; // okay, not really
50 CameraWidget *actualrootconfig;
52 retval = gp_camera_get_config(canon, &rootconfig, canoncontext);
53 actualrootconfig = rootconfig;
54 printf(" Retval: %d\n", retval);
56 printf("Get main config.\n");
58 retval = gp_widget_get_child_by_name(rootconfig, "main", &child);
59 printf(" Retval: %d\n", retval);
61 printf("Get settings config.\n");
63 retval = gp_widget_get_child_by_name(rootconfig, "settings", &child);
64 printf(" Retval: %d\n", retval);
66 printf("Get capturetarget.\n");
68 retval = gp_widget_get_child_by_name(rootconfig, "capturetarget", &child);
69 printf(" Retval: %d\n", retval);
72 CameraWidget *capture = child;
74 const char *widgetinfo;
75 gp_widget_get_name(capture, &widgetinfo);
76 printf("config name: %s\n", widgetinfo );
78 const char *widgetlabel;
79 gp_widget_get_label(capture, &widgetlabel);
80 printf("config label: %s\n", widgetlabel);
83 gp_widget_get_id(capture, &widgetid);
84 printf("config id: %d\n", widgetid);
86 CameraWidgetType widgettype;
87 gp_widget_get_type(capture, &widgettype);
88 printf("config type: %d == %d \n", widgettype, GP_WIDGET_RADIO);
91 printf("Set value.\n");
93 // capture to ram should be 0, although I think the filename also plays into
96 retval = gp_widget_set_value(capture, &one);
97 printf(" Retval: %d\n", retval);
99 printf("Enabling capture to CF.\n");
100 retval = gp_camera_set_config(canon, actualrootconfig, canoncontext);
101 printf(" Retval: %d\n", retval);
106 capture_to_file(Camera *canon, GPContext *canoncontext, char *fn) {
108 CameraFile *canonfile;
109 CameraFilePath camera_file_path;
111 printf("Capturing.\n");
113 /* NOP: This gets overridden in the library to /capt0000.jpg */
114 strcpy(camera_file_path.folder, "/");
115 strcpy(camera_file_path.name, "foo.jpg");
117 retval = gp_camera_capture(canon, GP_CAPTURE_IMAGE, &camera_file_path, canoncontext);
118 printf(" Retval: %d\n", retval);
120 printf("Pathname on the camera: %s/%s\n", camera_file_path.folder, camera_file_path.name);
122 fd = open(fn, O_CREAT | O_WRONLY, 0644);
123 retval = gp_file_new_from_fd(&canonfile, fd);
124 printf(" Retval: %d\n", retval);
125 retval = gp_camera_file_get(canon, camera_file_path.folder, camera_file_path.name,
126 GP_FILE_TYPE_NORMAL, canonfile, canoncontext);
127 printf(" Retval: %d\n", retval);
129 printf("Deleting.\n");
130 retval = gp_camera_file_delete(canon, camera_file_path.folder, camera_file_path.name,
132 printf(" Retval: %d\n", retval);
134 gp_file_free(canonfile);
138 main(int argc, char **argv) {
141 GPContext *canoncontext = sample_create_context();
143 gettimeofday(&starttime,NULL);
144 gp_log_add_func(GP_LOG_ERROR, errordumper, NULL);
145 gp_camera_new(&canon);
147 /* When I set GP_LOG_DEBUG instead of GP_LOG_ERROR above, I noticed that the
148 * init function seems to traverse the entire filesystem on the camera. This
149 * is partly why it takes so long.
150 * (Marcus: the ptp2 driver does this by default currently.)
152 printf("Camera init. Takes about 10 seconds.\n");
153 retval = gp_camera_init(canon, canoncontext);
154 if (retval != GP_OK) {
155 printf(" Retval: %d\n", retval);
158 canon_enable_capture(canon, TRUE, canoncontext);
159 /*set_capturetarget(canon, canoncontext);*/
160 capture_to_file(canon, canoncontext, "foo.jpg");
161 gp_camera_exit(canon, canoncontext);