summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMarcus Meissner <marcus@jet.franken.de>2010-03-05 23:12:31 +0000
committerMarcus Meissner <marcus@jet.franken.de>2010-03-05 23:12:31 +0000
commit35611300e18529094b3612514bd8cac103ce7ad9 (patch)
tree71065e6fa547c0ccea7d5e8be9847b754f8fdb94 /examples
parentfaedfacfff330192689d5a7c8fa513ba354af669 (diff)
downloadlibgphoto2-35611300e18529094b3612514bd8cac103ce7ad9.tar.gz
added a minimalistic capture sample
git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@12733 67ed7778-7388-44ab-90cf-0a291f65f57c
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile.am3
-rw-r--r--examples/sample-capture.c121
2 files changed, 123 insertions, 1 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am
index e20fc2f83..4da77511a 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -21,12 +21,13 @@ INSTALL_TESTS_ENVIRONMENT = env \
CLEANFILES = $(check_SCRIPTS)
-noinst_PROGRAMS += sample-autodetect sample-multi-detect lunkwill-canon-capture sample-owner sample-preview
+noinst_PROGRAMS += sample-autodetect sample-multi-detect lunkwill-canon-capture sample-owner sample-preview sample-capture
sample_autodetect_SOURCES = sample-autodetect.c samples.h context.c config.c
sample_owner_SOURCES = sample-owner.c samples.h context.c config.c
sample_multi_detect_SOURCES = sample-multi-detect.c samples.h autodetect.c context.c config.c
lunkwill_canon_capture_SOURCES = lunkwill-canon-capture.c context.c samples.h config.c
+sample_capture_SOURCES = sample-capture.c context.c samples.h config.c
sample_preview_SOURCES = preview.c samples.h context.c config.c
LDADD = $(top_builddir)/libgphoto2/libgphoto2.la \
diff --git a/examples/sample-capture.c b/examples/sample-capture.c
new file mode 100644
index 000000000..5ace242a8
--- /dev/null
+++ b/examples/sample-capture.c
@@ -0,0 +1,121 @@
+/*
+ * This code released into the public domain 21 July 2008
+ *
+ * This program does the equivalent of:
+ * gphoto2 --shell
+ * > set-config capture=1
+ * > capture-image-and-download
+ * compile with gcc -Wall -o canon-capture -lgphoto2 canon-capture.c
+ *
+ * Taken from: http://credentiality2.blogspot.com/2008/07/linux-libgphoto2-image-capture-from.html
+ *
+ * and condensed into simple capture sample
+ */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <gphoto2/gphoto2.h>
+
+#include "samples.h"
+
+static void errordumper(GPLogLevel level, const char *domain, const char *format,
+ va_list args, void *data) {
+ vfprintf(stdout, format, args);
+ fprintf(stdout, "\n");
+}
+
+static void
+capture_to_memory(Camera *camera, GPContext *context, const char **ptr, unsigned long int *size) {
+ int retval;
+ CameraFile *file;
+ CameraFilePath camera_file_path;
+
+ printf("Capturing.\n");
+
+ /* NOP: This gets overridden in the library to /capt0000.jpg */
+ strcpy(camera_file_path.folder, "/");
+ strcpy(camera_file_path.name, "foo.jpg");
+
+ retval = gp_camera_capture(camera, GP_CAPTURE_IMAGE, &camera_file_path, context);
+ printf(" Retval: %d\n", retval);
+
+ printf("Pathname on the camera: %s/%s\n", camera_file_path.folder, camera_file_path.name);
+
+ retval = gp_file_new(&file);
+ printf(" Retval: %d\n", retval);
+ retval = gp_camera_file_get(camera, camera_file_path.folder, camera_file_path.name,
+ GP_FILE_TYPE_NORMAL, file, context);
+ printf(" Retval: %d\n", retval);
+
+ gp_file_get_data_and_size (file, ptr, size);
+
+ printf("Deleting.\n");
+ retval = gp_camera_file_delete(camera, camera_file_path.folder, camera_file_path.name,
+ context);
+ printf(" Retval: %d\n", retval);
+
+ gp_file_free(file);
+}
+
+static void
+capture_to_file(Camera *camera, GPContext *context, char *fn) {
+ int fd, retval;
+ CameraFile *file;
+ CameraFilePath camera_file_path;
+
+ printf("Capturing.\n");
+
+ /* NOP: This gets overridden in the library to /capt0000.jpg */
+ strcpy(camera_file_path.folder, "/");
+ strcpy(camera_file_path.name, "foo.jpg");
+
+ retval = gp_camera_capture(camera, GP_CAPTURE_IMAGE, &camera_file_path, context);
+ printf(" Retval: %d\n", retval);
+
+ printf("Pathname on the camera: %s/%s\n", camera_file_path.folder, camera_file_path.name);
+
+ fd = open(fn, O_CREAT | O_WRONLY, 0644);
+ retval = gp_file_new_from_fd(&file, fd);
+ printf(" Retval: %d\n", retval);
+ retval = gp_camera_file_get(camera, camera_file_path.folder, camera_file_path.name,
+ GP_FILE_TYPE_NORMAL, file, context);
+ printf(" Retval: %d\n", retval);
+
+ printf("Deleting.\n");
+ retval = gp_camera_file_delete(camera, camera_file_path.folder, camera_file_path.name,
+ context);
+ printf(" Retval: %d\n", retval);
+
+ gp_file_free(file);
+}
+
+int
+main(int argc, char **argv) {
+ Camera *camera;
+ int retval;
+ GPContext *context = sample_create_context();
+
+ gp_log_add_func(GP_LOG_ERROR, errordumper, NULL);
+ gp_camera_new(&camera);
+
+ /* When I set GP_LOG_DEBUG instead of GP_LOG_ERROR above, I noticed that the
+ * init function seems to traverse the entire filesystem on the camera. This
+ * is partly why it takes so long.
+ * (Marcus: the ptp2 driver does this by default currently.)
+ */
+ printf("Camera init. Takes about 10 seconds.\n");
+ retval = gp_camera_init(camera, context);
+ if (retval != GP_OK) {
+ printf(" Retval: %d\n", retval);
+ exit (1);
+ }
+ capture_to_file(camera, context, "foo.jpg");
+ gp_camera_exit(camera, context);
+ return 0;
+}