Libexif, adding new exif data

I have a task to edit exif tags and add specific application values โ€‹โ€‹to them. if exif tags exist libexif are more than happy to edit them. but if the exif tags do not exist, I will have to create them and add to the file. libexif uses C fopen, so I donโ€™t think it would be an easy way without some IO manipulation. I think to read the raw image data placed in memory, fopen (newfile, 'w') add exif data and then add image data.
only if someone knows an easier way (I'm limited to libexif, libexiv2 can create a conflict with liscence).

+6
source share
2 answers

for the common good, I'm going to answer my question, the exif application has a modified libjpeg that allows you to manipulate jpeg source data. it has features like

jpeg_data_load_data (JPEGData *data, const unsigned char *d,unsigned int size); and

jpeg_data_set_exif_data(myJPEGImage,exif); jpeg_data_save_file(myJPEGImage,"gangrene1.jpg");

This can be used, as well as free available programs like imagemagick, have their own implementation of libjpeg, libexif to manipulate exif and jpeg data. Hope this helps

+5
source

I just went the same way as you, choosing between libexif and libexiv2. I went with libexif due to licensing.

Back to the question

libexif does not support direct download of jpg. You will need another package to read in JPG and extract the EXIF โ€‹โ€‹header (or you can write something yourself).

There is a great Github project called exifyay that uses libexif and has two additional libraries that handle reading in JPGS. This is a python project, but the source for libraries is C. You can find exifyay here (note that I am in no way involved in exifyay or libexif)

I just compiled libexif and combined the sources from exifyay into the VS2010 project here . There is an example in the folder "contrib \ examples \ LibexifExample". If you donโ€™t like downloading random links, here is an example of the code I received:

 /* * write-exif.c * * Placed into the public domain by Daniel Fandrich * * Create a new EXIF data block and write it into a JPEG image file. * * The JPEG image data used in this example is fixed and is guaranteed not * to contain an EXIF tag block already, so it is easy to precompute where * in the file the EXIF data should be. In real life, a library like * libjpeg (included with the exif command-line tool source code) would * be used to write to an existing JPEG file. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include <libexif/exif-data.h> #include <libjpeg/jpeg-data.h> #include <JpegEncoderEXIF/JpegEncoderEXIF.h> /* byte order to use in the EXIF block */ #define FILE_BYTE_ORDER EXIF_BYTE_ORDER_INTEL /* comment to write into the EXIF block */ #define FILE_COMMENT "libexif demonstration image" /* special header required for EXIF_TAG_USER_COMMENT */ #define ASCII_COMMENT "ASCII\0\0\0" static ExifEntry *create_tag(ExifData *exif, ExifIfd ifd, ExifTag tag, size_t len) { void *buf; ExifEntry *entry; /* Create a memory allocator to manage this ExifEntry */ ExifMem *mem = exif_mem_new_default(); assert(mem != NULL); /* catch an out of memory condition */ /* Create a new ExifEntry using our allocator */ entry = exif_entry_new_mem (mem); assert(entry != NULL); /* Allocate memory to use for holding the tag data */ buf = exif_mem_alloc(mem, len); assert(buf != NULL); /* Fill in the entry */ entry->data = (unsigned char*)buf; entry->size = len; entry->tag = tag; entry->components = len; entry->format = EXIF_FORMAT_UNDEFINED; /* Attach the ExifEntry to an IFD */ exif_content_add_entry (exif->ifd[ifd], entry); /* The ExifMem and ExifEntry are now owned elsewhere */ exif_mem_unref(mem); exif_entry_unref(entry); return entry; } int main(int argc, char **argv) { ExifEntry *entry; //Input JPG char mInputFilename[]="example.jpg"; //Load JPG JPEGData * mJpegData = jpeg_data_new_from_file(mInputFilename); //Load Exif data from JPG ExifData * mExifData = jpeg_data_get_exif_data(mJpegData); //Set some Exif options exif_data_set_option(mExifData, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION); exif_data_set_data_type(mExifData, EXIF_DATA_TYPE_COMPRESSED); exif_data_set_byte_order(mExifData, FILE_BYTE_ORDER); entry = create_tag(mExifData, EXIF_IFD_EXIF, EXIF_TAG_USER_COMMENT, sizeof(ASCII_COMMENT) + sizeof(FILE_COMMENT) - 2); /* Write the special header needed for a comment tag */ memcpy(entry->data, ASCII_COMMENT, sizeof(ASCII_COMMENT)-1); /* Write the actual comment text, without the trailing NUL character */ memcpy(entry->data+8, FILE_COMMENT, sizeof(FILE_COMMENT)-1); /* create_tag() happens to set the format and components correctly for * EXIF_TAG_USER_COMMENT, so there is nothing more to do. */ /* Create a EXIF_TAG_SUBJECT_AREA tag */ entry = create_tag(mExifData, EXIF_IFD_EXIF, EXIF_TAG_SUBJECT_AREA, 4 * exif_format_get_size(EXIF_FORMAT_SHORT)); entry->format = EXIF_FORMAT_SHORT; entry->components = 4; //Write back exif data jpeg_data_set_exif_data(mJpegData,mExifData); //Save to JPG jpeg_data_save_file(mJpegData,"test.jpg"); return 0; } 
0
source

Source: https://habr.com/ru/post/923392/


All Articles