2013-01-15 57 views
2

我有一個使用BlackBerry 10 Dev Alpha拍攝的JPG文件。這個代碼(的this example略加修改)輸出結果正確如何編輯C中的EXIF標籤?

static char* read_tag(ExifData *ed, ExifIfd eid, ExifTag tag){ 
static char result[1024]; 
ExifEntry *entry = exif_content_get_entry(ed->ifd[eid], tag); 

if (entry){ 
    char buf[1024]; 

    exif_entry_get_value(entry, buf, sizeof(buf)); 
    trim_spaces(buf); 

    if (*buf) strcpy(result, buf); 
    else strcpy(result, "NULL"); 
} 
else strcpy(result, "NULL"); 

return result; 
} 

這意味着的輸出:

printf("Model : %s\n", read_tag(ed, EXIF_IFD_0, EXIF_TAG_MODEL)); 

是:

型號:黑莓10開發阿爾法

現在我不知道如何將「BlackBerry 10 Dev Al pha「(EXIF_TAG_MODEL)與另一個值,例如」Nokia 3330「。我已經看過another example。不幸的是我發現它很難閱讀。也許有人有一個簡短/直接的代碼?

+0

10你覺得很難閱讀的鏈接示例的哪一部分?如果你給一些行號,我會看看我能做些什麼。 –

+0

我想,從第263行開始。但當然這是關於寫一個新的標籤。我想要的是編輯現有的標籤。 – anta40

回答

2

libexif不支持直接加載JPG文件。您需要另外一個包來閱讀JPG文件並提取EXIF文件頭(或者您可以自己寫一些東西)。

注意,在這個例子中它只需創建一個新的EXIF頭,然後將其保存使用fwrite來的文件,然後附加在這裏這部分代碼的不就完了EXIF信息的原始JPG數據:

/* Write JPEG image data, skipping the non-EXIF header */ 
      if (fwrite(image_jpg+image_data_offset, image_data_len, 1, f) != 1) { 
        fprintf(stderr, "Error writing to file %s\n", FILE_NAME); 
        goto errout; 
      } 

有一個很好的Github項目叫做exifyay,它使用了libexif,並有兩個額外的庫來處理JPGS中的讀取。這是一個python項目,但庫的來源是C.你可以找到exifyay here(注意我沒有以任何方式與exifyay或libexif)

我剛剛編譯libexif和合並來源exifyay到VS2010項目here。文件夾'contrib \ examples \ LibexifExample'中有一個示例。如果你不喜歡下載隨機鏈接,這裏是我工作的代碼示例:

/* 
* 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

「libexif不支持直接將JPG加載到」 - exif_data_new_from_file()'函數中。 –