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>
21 #include <gphoto2/gphoto2.h>
25 static void errordumper(GPLogLevel level, const char *domain, const char *format,
26 va_list args, void *data) {
27 vfprintf(stdout, format, args);
28 fprintf(stdout, "\n");
31 /* This seems to have no effect on where images go
32 void set_capturetarget(Camera *canon, GPContext *canoncontext) {
34 printf("Get root config.\n");
35 CameraWidget *rootconfig; // okay, not really
36 CameraWidget *actualrootconfig;
38 retval = gp_camera_get_config(canon, &rootconfig, canoncontext);
39 actualrootconfig = rootconfig;
40 printf(" Retval: %d\n", retval);
42 printf("Get main config.\n");
44 retval = gp_widget_get_child_by_name(rootconfig, "main", &child);
45 printf(" Retval: %d\n", retval);
47 printf("Get settings config.\n");
49 retval = gp_widget_get_child_by_name(rootconfig, "settings", &child);
50 printf(" Retval: %d\n", retval);
52 printf("Get capturetarget.\n");
54 retval = gp_widget_get_child_by_name(rootconfig, "capturetarget", &child);
55 printf(" Retval: %d\n", retval);
58 CameraWidget *capture = child;
60 const char *widgetinfo;
61 gp_widget_get_name(capture, &widgetinfo);
62 printf("config name: %s\n", widgetinfo );
64 const char *widgetlabel;
65 gp_widget_get_label(capture, &widgetlabel);
66 printf("config label: %s\n", widgetlabel);
69 gp_widget_get_id(capture, &widgetid);
70 printf("config id: %d\n", widgetid);
72 CameraWidgetType widgettype;
73 gp_widget_get_type(capture, &widgettype);
74 printf("config type: %d == %d \n", widgettype, GP_WIDGET_RADIO);
77 printf("Set value.\n");
79 // capture to ram should be 0, although I think the filename also plays into
82 retval = gp_widget_set_value(capture, &one);
83 printf(" Retval: %d\n", retval);
85 printf("Enabling capture to CF.\n");
86 retval = gp_camera_set_config(canon, actualrootconfig, canoncontext);
87 printf(" Retval: %d\n", retval);
92 capture_to_file(Camera *canon, GPContext *canoncontext, char *fn) {
94 CameraFile *canonfile;
95 CameraFilePath camera_file_path;
97 printf("Capturing.\n");
99 retval = gp_camera_capture(canon, GP_CAPTURE_IMAGE, &camera_file_path, canoncontext);
100 printf(" Retval: %d\n", retval);
102 printf("Pathname on the camera: %s/%s\n", camera_file_path.folder, camera_file_path.name);
104 fd = open(fn, O_CREAT | O_WRONLY, 0644);
105 retval = gp_file_new_from_fd(&canonfile, fd);
106 printf(" Retval: %d\n", retval);
107 retval = gp_camera_file_get(canon, camera_file_path.folder, camera_file_path.name,
108 GP_FILE_TYPE_NORMAL, canonfile, canoncontext);
109 printf(" Retval: %d\n", retval);
111 printf("Deleting.\n");
112 retval = gp_camera_file_delete(canon, camera_file_path.folder, camera_file_path.name,
114 printf(" Retval: %d\n", retval);
116 gp_file_free(canonfile);
120 main(int argc, char **argv) {
123 GPContext *canoncontext = sample_create_context();
125 gp_log_add_func(GP_LOG_ERROR, errordumper, NULL);
126 gp_camera_new(&canon);
128 /* When I set GP_LOG_DEBUG instead of GP_LOG_ERROR above, I noticed that the
129 * init function seems to traverse the entire filesystem on the camera. This
130 * is partly why it takes so long.
131 * (Marcus: the ptp2 driver does this by default currently.)
133 printf("Camera init. Takes about 10 seconds.\n");
134 retval = gp_camera_init(canon, canoncontext);
135 if (retval != GP_OK) {
136 printf(" Retval: %d\n", retval);
139 canon_enable_capture(canon, TRUE, canoncontext);
140 /*set_capturetarget(canon, canoncontext);*/
141 printf("Taking 100 previews and saving them to snapshot-XXX.jpg ...\n");
144 char output_file[32];
146 fprintf(stderr,"preview %d\n", i);
147 retval = gp_file_new(&file);
148 if (retval != GP_OK) {
149 fprintf(stderr,"gp_file_new: %d\n", retval);
152 retval = gp_camera_capture_preview(canon, file, canoncontext);
153 if (retval != GP_OK) {
154 fprintf(stderr,"gp_camera_capture_preview(%d): %d\n", i, retval);
157 sprintf(output_file, "snapshot-%03d.jpg", i);
158 retval = gp_file_save(file, output_file);
159 if (retval != GP_OK) {
160 fprintf(stderr,"gp_camera_capture_preview(%d): %d\n", i, retval);
164 /* if you want to capture right afterwards
165 sprintf(output_file, "image-%03d.jpg", i);
166 capture_to_file(canon, canoncontext, output_file);
169 gp_camera_exit(canon, canoncontext);