Source code upload
[framework/connectivity/libgphoto2.git] / examples / preview.c
1 /* compile with gcc -Wall -o canon-capture -lgphoto2 canon-capture.c
2  * This code released into the public domain 21 July 2008
3  * 
4  * This program does the equivalent of:
5  * gphoto2 --shell
6  *   > set-config capture=1
7  *   > capture-image-and-download
8  * compile with gcc -Wall -o canon-capture -lgphoto2 canon-capture.c
9  *
10  * Taken from: http://credentiality2.blogspot.com/2008/07/linux-libgphoto2-image-capture-from.html 
11  */
12
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <string.h>
21 #include <gphoto2/gphoto2.h>
22
23 #include "samples.h"
24
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");
29 }
30
31 /* This seems to have no effect on where images go
32 void set_capturetarget(Camera *canon, GPContext *canoncontext) {
33         int retval;
34         printf("Get root config.\n");
35         CameraWidget *rootconfig; // okay, not really
36         CameraWidget *actualrootconfig;
37
38         retval = gp_camera_get_config(canon, &rootconfig, canoncontext);
39         actualrootconfig = rootconfig;
40         printf("  Retval: %d\n", retval);
41
42         printf("Get main config.\n");
43         CameraWidget *child;
44         retval = gp_widget_get_child_by_name(rootconfig, "main", &child);
45         printf("  Retval: %d\n", retval);
46
47         printf("Get settings config.\n");
48         rootconfig = child;
49         retval = gp_widget_get_child_by_name(rootconfig, "settings", &child);
50         printf("  Retval: %d\n", retval);
51
52         printf("Get capturetarget.\n");
53         rootconfig = child;
54         retval = gp_widget_get_child_by_name(rootconfig, "capturetarget", &child);
55         printf("  Retval: %d\n", retval);
56
57
58         CameraWidget *capture = child;
59
60         const char *widgetinfo;
61         gp_widget_get_name(capture, &widgetinfo);
62         printf("config name: %s\n", widgetinfo );
63
64         const char *widgetlabel;
65         gp_widget_get_label(capture, &widgetlabel);
66         printf("config label: %s\n", widgetlabel);
67
68         int widgetid;
69         gp_widget_get_id(capture, &widgetid);
70         printf("config id: %d\n", widgetid);
71
72         CameraWidgetType widgettype;
73         gp_widget_get_type(capture, &widgettype);
74         printf("config type: %d == %d \n", widgettype, GP_WIDGET_RADIO);
75
76
77         printf("Set value.\n");
78
79         // capture to ram should be 0, although I think the filename also plays into
80         // it
81         int one=1;
82         retval = gp_widget_set_value(capture, &one);
83         printf("  Retval: %d\n", retval);
84
85         printf("Enabling capture to CF.\n");
86         retval = gp_camera_set_config(canon, actualrootconfig, canoncontext);
87         printf("  Retval: %d\n", retval);
88 }
89 */
90
91 static void
92 capture_to_file(Camera *canon, GPContext *canoncontext, char *fn) {
93         int fd, retval;
94         CameraFile *canonfile;
95         CameraFilePath camera_file_path;
96
97         printf("Capturing.\n");
98
99         retval = gp_camera_capture(canon, GP_CAPTURE_IMAGE, &camera_file_path, canoncontext);
100         printf("  Retval: %d\n", retval);
101
102         printf("Pathname on the camera: %s/%s\n", camera_file_path.folder, camera_file_path.name);
103
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);
110
111         printf("Deleting.\n");
112         retval = gp_camera_file_delete(canon, camera_file_path.folder, camera_file_path.name,
113                         canoncontext);
114         printf("  Retval: %d\n", retval);
115
116         gp_file_free(canonfile);
117 }
118
119 int
120 main(int argc, char **argv) {
121         Camera  *canon;
122         int     i, retval;
123         GPContext *canoncontext = sample_create_context();
124
125         gp_log_add_func(GP_LOG_ERROR, errordumper, NULL);
126         gp_camera_new(&canon);
127
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.)
132          */
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);
137                 exit (1);
138         }
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");
142         for (i=0;i<99;i++) {
143                 CameraFile *file;
144                 char output_file[32];
145
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);
150                         exit(1);
151                 }
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);
155                         exit(1);
156                 }
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);
161                         exit(1);
162                 }
163                 gp_file_unref(file);
164 /* if you want to capture right afterwards 
165                 sprintf(output_file, "image-%03d.jpg", i);
166                 capture_to_file(canon, canoncontext, output_file);
167 */
168         }
169         gp_camera_exit(canon, canoncontext);
170         return 0;
171 }