Home | History | Annotate | Download | only in examples
      1 /*
      2  * libexif example program to extract an EXIF thumbnail from an image
      3  * and save it into a new file.
      4  *
      5  * Placed into the public domain by Dan Fandrich
      6  */
      7 
      8 #include <stdio.h>
      9 #include <libexif/exif-loader.h>
     10 
     11 int main(int argc, char **argv)
     12 {
     13     int rc = 1;
     14     ExifLoader *l;
     15 
     16     if (argc < 2) {
     17         printf("Usage: %s image.jpg\n", argv[0]);
     18         printf("Extracts a thumbnail from the given EXIF image.\n");
     19         return rc;
     20     }
     21 
     22     /* Create an ExifLoader object to manage the EXIF loading process */
     23     l = exif_loader_new();
     24     if (l) {
     25         ExifData *ed;
     26 
     27         /* Load the EXIF data from the image file */
     28         exif_loader_write_file(l, argv[1]);
     29 
     30         /* Get a pointer to the EXIF data */
     31         ed = exif_loader_get_data(l);
     32 
     33 	/* The loader is no longer needed--free it */
     34         exif_loader_unref(l);
     35 	l = NULL;
     36         if (ed) {
     37 	    /* Make sure the image had a thumbnail before trying to write it */
     38             if (ed->data && ed->size) {
     39                 FILE *thumb;
     40                 char thumb_name[1024];
     41 
     42 		/* Try to create a unique name for the thumbnail file */
     43                 snprintf(thumb_name, sizeof(thumb_name),
     44                          "%s_thumb.jpg", argv[1]);
     45 
     46                 thumb = fopen(thumb_name, "wb");
     47                 if (thumb) {
     48 		    /* Write the thumbnail image to the file */
     49                     fwrite(ed->data, 1, ed->size, thumb);
     50                     fclose(thumb);
     51                     printf("Wrote thumbnail to %s\n", thumb_name);
     52                     rc = 0;
     53                 } else {
     54                     printf("Could not create file %s\n", thumb_name);
     55                     rc = 2;
     56                 }
     57             } else {
     58                 printf("No EXIF thumbnail in file %s\n", argv[1]);
     59                 rc = 1;
     60             }
     61 	    /* Free the EXIF data */
     62             exif_data_unref(ed);
     63         }
     64     }
     65     return rc;
     66 }
     67