Skip to content

Instantly share code, notes, and snippets.

@jemygraw
Created August 29, 2017 07:37
Show Gist options
  • Save jemygraw/890012a1636b3594c49e62ecc588e1c7 to your computer and use it in GitHub Desktop.
Save jemygraw/890012a1636b3594c49e62ecc588e1c7 to your computer and use it in GitHub Desktop.
JPEG file IPTC meta info read example
#include <libiptcdata/iptc-data.h>
#include <libiptcdata/iptc-jpeg.h>
#include <jpeglib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void iptc_dataset_foreach_handler(IptcDataSet *dataset, void *datasetName) {
IptcFormat format = iptc_dataset_get_format(dataset);
printf("filter: %s\n\n", (char *) datasetName);
//add filter
// if (strcmp(dataset->info->name, datasetName) != 0) {
// printf("ignore this dataset %s\n", dataset->info->name);
// return;
// }
char *fmtStr = "unknown";
switch (format) {
case IPTC_FORMAT_BINARY:
fmtStr = "binary";
break;
case IPTC_FORMAT_BYTE:
fmtStr = "byte";
break;
case IPTC_FORMAT_SHORT:
fmtStr = "short";
break;
case IPTC_FORMAT_LONG:
fmtStr = "long";
break;
case IPTC_FORMAT_STRING:
fmtStr = "string";
break;
case IPTC_FORMAT_NUMERIC_STRING:
fmtStr = "numeric string";
break;
case IPTC_FORMAT_DATE:
fmtStr = "date";
break;
case IPTC_FORMAT_TIME:
fmtStr = "time";
break;
default:
fmtStr = "unknown";
break;
}
printf("format: %s\n", fmtStr);
printf("info: %s, %s, %s\n", dataset->info->name, dataset->info->title, dataset->info->description);
printf("data: %s, size: %d, tag: %d, record: %d\n", dataset->data, dataset->size, dataset->tag, dataset->record);
printf("\n");
iptc_dataset_unref(dataset);
}
int main(int argc, char **argv) {
char *jpegFile = "/Users/jemy/XLab/iptc/iptc-base.jpg";
IptcData *iptcData = iptc_data_new_from_jpeg(jpegFile);
if (iptcData) {
printf("image has iptc data: %d\n", iptcData->count);
//iptc_data_foreach_dataset(iptcData, iptc_dataset_foreach_handler, "Keywords");
//add new dataset
IptcDataSet *newDataSet = iptc_dataset_new();
iptc_dataset_set_tag(newDataSet, IPTC_RECORD_APP_2, IPTC_TAG_CITY);
iptc_dataset_set_data(newDataSet, (unsigned char *) "上海", (unsigned int) strlen("上海"), IPTC_VALIDATE);
int success = iptc_data_add_dataset(iptcData, newDataSet);
printf("add status: %d\n", success);
iptc_data_foreach_dataset(iptcData, iptc_dataset_foreach_handler, "City");
} else {
printf("image has no iptc data\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment