Libexif example to add a small Xml document to exif data

Any libexif user / developer who can point me in the right direction on what is a suitable call to add a small custom XML document to the exif metadata of a JPG image?

I was looking for time to search and can not understand.

I am open to any other open source library that will allow me to do this while it is based on C.

+3
source share
2 answers

You can insert any data into an EXIF ​​data block in a JPG. There is no size limit that I know of, you just split it into several EXIF ​​data blocks if necessary.

libexif. C-, ( , JPG, EXIF-):

, JPG ( EXIF) - :

0xFF 0xD8 

Exif :

0xFF 0xE0 

Exif, :

0x00 0x10 //In this case it is 16 bytes (0x0010) long and it INCLUDES these two bytes of the header

Exif ( ). , 14 ( 14+ 2= 16bytes 0x0010, ):

0x4A 0x46 0x49 0x46 0x00 0x01 0x01 0x01 0x00 0x60 0x00 0x60 0x00 0x00 

XML Exif , exif- ( ):

0xFF 0xE1

XML + 2 ( , 0xFFFE, EXIF):

0x07 0x7D //In this case it is 1917bytes long or 0x077D

xml JPG .

. ( , ):

Example of XML file inserted into EXIF ​​data of JPG

JPG XML . , , Hex :

JPG XML EXIF ​​

, JPG, .

++ (, , , !):

char yourdata[]="<xml> contents to </add>";
long yourdatalen = 0x18;

//open file
char * file;
long filelen=0;
std::ifstream infile;
infile.open("yourjpg.jpg",std::ios::binary| std::ios::in);

//find size of file
infile.seekg (0, ios::end);
filelen = infile.tellg();
infile.seekg (0, ios::beg);

//read contents of file
file = new char [filelen];
infile.read(file,filelen);
infile.close();

//lets parse through the file and find any exif headers
long x=0;
if ((file[0]==0xFF) && (file[1]==0xD8)){

    //all good lets go!!
    while ((file[x]!=0xFF) && (file[x+1]!=0xE1)) {
       x++;
    }

    //were at the first EXIF data block! insert XML here
    char * temp=file;
    file = new char [filelen+yourdatalen+4];
    memcpy(file,temp,x);
    file[x+0]=0xFF;
    file[x+1]=0xE1;
    file[x+2]=int((yourdatalen+2)/0xFF); //note assumes that your xml file is less than 0xFFFE bytes long
    file[x+3]=yourdatalen-int((yourdatalen+2)/0xFF);
    memcpy(&file[x+4],yourdata,yourdatalen);
    memcpy(&file[x+4+yourdatalen],temp[x],filelen-x);
    delete [] temp;

    //Save to file
    std::ofstream ofile;
    ofile.open("savejpg.jpg",std::ios::binary| std::ios::out);
    ofile.write(file,yourdatalen+4+filelen);
    ofile.close();
}
else {
    //JPG file does not have exif data in it, you'll need to add it first or find another way of adding your data
}

//Clean up
delete [] file;

HxD, .

+2

, EXIF ​​ 32 , XML .

ASCII C-string XML, , libxml2. EXIF ​​ " " .

: http://libexif.cvs.sourceforge.net/viewvc/libexif/libexif/contrib/examples/write-exif.c?view=markup

, ASCII_COMMENT, FILE_COMMENT - XML-.

0

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


All Articles