Skip to content

Instantly share code, notes, and snippets.

@vlussenburg
Last active May 29, 2024 22:43
Show Gist options
  • Save vlussenburg/01783282f5c813bf59669c2b1319f19b to your computer and use it in GitHub Desktop.
Save vlussenburg/01783282f5c813bf59669c2b1319f19b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include "libexif/exif-data.h"
// Function to log messages
void log_message(const char *msg) {
printf("%s\n", msg);
}
// Function to print an Exif entry
void print_exif_entry(ExifEntry *entry, ExifIfd ifd) {
char value[1024];
exif_entry_get_value(entry, value, sizeof(value));
printf(" Tag: %s\n", exif_tag_get_name_in_ifd(entry->tag, ifd));
printf(" Value: %s\n", value);
}
// Function to print the Exif content
void print_exif_content(ExifContent *content, ExifIfd ifd) {
for (unsigned int i = 0; i < content->count; i++) {
print_exif_entry(content->entries[i], ifd);
}
}
// Function to read EXIF data from a file and print it
void read_exif_data(const char *filename) {
ExifData *ed;
ExifByteOrder byte_order;
// Load the EXIF data from the file
ed = exif_data_new_from_file(filename);
if (!ed) {
log_message("Could not read EXIF data.");
return;
}
// Get the byte order of the EXIF data
byte_order = exif_data_get_byte_order(ed);
log_message("EXIF data byte order:");
if (byte_order == EXIF_BYTE_ORDER_INTEL) {
log_message("Intel (Little endian)");
} else {
log_message("Motorola (Big endian)");
}
// Loop through and print out the tags
for (int i = 0; i < EXIF_IFD_COUNT; i++) {
ExifIfd ifd = (ExifIfd)i;
ExifContent *content = ed->ifd[ifd];
if (!content || content->count == 0) {
continue;
}
log_message("EXIF content:");
print_exif_content(content, ifd);
}
// Free the EXIF data
exif_data_unref(ed);
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <JPEG file>\n", argv[0]);
return 1;
}
const char *filename = argv[1];
read_exif_data(filename);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment