$search
00001 /********************************************************************* 00002 * 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2009, Robert Bosch LLC. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of the Robert Bosch nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 *********************************************************************/ 00036 #include <unistd.h> 00037 #include <stdlib.h> 00038 #include <stdio.h> 00039 #include <string.h> 00040 #include <fcntl.h> 00041 #include <photo/photo.h> 00042 #include <photo/photo_image.h> 00043 00044 int photo_capture_to_file(photo_p photo, const char *filename) 00045 { 00046 int fd, ret; 00047 CameraFile *photofile; 00048 CameraFilePath photo_file_path; 00049 00050 /* NOP: This gets overridden in the library to /capt0000.jpg */ 00051 strcpy(photo_file_path.folder, "/"); 00052 strcpy(photo_file_path.name, "foo.jpg"); 00053 00054 ret = gp_camera_capture(photo->cam, GP_CAPTURE_IMAGE, &photo_file_path, photo->context); 00055 if (ret<GP_OK) { 00056 fprintf(stderr, "Could not capture image (error code %d)\n", ret); 00057 return ret; 00058 } 00059 00060 fd = open(filename, O_CREAT|O_WRONLY, 0644); 00061 ret = gp_file_new_from_fd(&photofile, fd); 00062 if (ret<GP_OK) { 00063 fprintf(stderr, "Could not create a new image file from %s%s (error code %d)\n", photo_file_path.folder,photo_file_path.name,ret); 00064 goto out; 00065 } 00066 00067 ret = gp_camera_file_get(photo->cam, photo_file_path.folder, photo_file_path.name, GP_FILE_TYPE_NORMAL, photofile, photo->context); 00068 if (ret<GP_OK) { 00069 fprintf(stderr, "Could not get file %s%s (error code %d)\n", photo_file_path.folder,photo_file_path.name,ret); 00070 // goto out; 00071 } 00072 00073 ret = gp_camera_file_delete(photo->cam, photo_file_path.folder, photo_file_path.name, photo->context); 00074 if (ret<GP_OK) { 00075 fprintf(stderr, "Could delete file %s%s (error code %d)\n", photo_file_path.folder,photo_file_path.name,ret); 00076 goto out; 00077 } 00078 00079 out: gp_file_free(photofile); 00080 return (ret==GP_OK); 00081 } 00082 00083 00084 int photo_capture(photo_p photo, photo_image_p image) 00085 { 00086 int fd, ret; 00087 CameraFile *photofile; 00088 CameraFilePath photo_file_path; 00089 char tmpname[20]; 00090 00091 /* NOP: This gets overridden in the library to /capt0000.jpg */ 00092 strcpy(photo_file_path.folder, "/"); 00093 strcpy(photo_file_path.name, "foo.jpg"); 00094 00095 ret = gp_camera_capture(photo->cam, GP_CAPTURE_IMAGE, &photo_file_path, photo->context); 00096 if (ret<GP_OK) { 00097 fprintf(stderr, "Could not capture image (error code %d)\n", ret); 00098 return ret; 00099 } 00100 00101 /* create temporary file */ 00102 strcpy (tmpname, "tmpfileXXXXXX"); 00103 fd = mkstemp(tmpname); 00104 ret = gp_file_new_from_fd (&photofile, fd); 00105 if (ret < GP_OK) { 00106 close (fd); 00107 unlink(tmpname); 00108 return ret; 00109 } 00110 00111 /* get image from camera */ 00112 ret = gp_camera_file_get (photo->cam, photo_file_path.folder, photo_file_path.name, GP_FILE_TYPE_NORMAL, photofile, photo->context); 00113 if (ret < GP_OK) { 00114 gp_file_unref(photofile); 00115 unlink(tmpname); 00116 return ret; 00117 } 00118 00119 ret = gp_camera_file_delete(photo->cam, photo_file_path.folder, photo_file_path.name, photo->context); 00120 if (ret<GP_OK) { 00121 fprintf(stderr, "Could delete file %s%s (error code %d)\n", photo_file_path.folder,photo_file_path.name,ret); 00122 goto out; 00123 } 00124 00125 if(photo_image_read(image,tmpname)) 00126 ret=GP_OK; 00127 else 00128 ret=-1; 00129 00130 out: 00131 gp_file_free(photofile); 00132 unlink(tmpname); 00133 return (ret==GP_OK); 00134 } 00135 00136 00137