首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >libexif:使用IPTC/XMP将新的exif写入镜像

libexif:使用IPTC/XMP将新的exif写入镜像
EN

Stack Overflow用户
提问于 2013-06-10 16:11:48
回答 1查看 1.2K关注 0票数 1

我正在尝试使用libexif将EXIF元数据写入JPEG图像。我遇到过没有EXIF的图像,但有IPTC/XMP元数据的问题。在使用IPTC/XMP将EXIF写入图像后,我无法读取EXIF。" EXIF“命令行实用程序(也使用libexif)报告EXIF损坏。如果我对没有任何元数据的图像执行相同的操作,则一切正常。

这是一个演示问题的小测试应用程序(它假设我们正在处理没有EXIF的图像)。

代码语言:javascript
复制
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include <libexif/exif-data.h>
#include <libexif/exif-loader.h>
#include <jpeglib.h>

#include "jpegexif/jpeg-data.h"
#include "jpegexif/jpeg-marker.h"
#include "jpegexif/jpeg-data.c"
#include "jpegexif/jpeg-marker.c"



static ExifEntry *create_ascii_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();

    // Create a new ExifEntry using our allocator.
    entry = exif_entry_new_mem(mem);

    // Allocate memory to use for holding the tag data.
    buf = exif_mem_alloc(mem, len);

    // Fill in the entry.
    entry->data = (unsigned char*) buf;
    entry->size = len;
    entry->tag = tag;
    entry->components = len;
    entry->format = EXIF_FORMAT_ASCII;

    // 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 create_exif_and_set_software(const char* const filename, const char* const tagvalue) {
    // For simplification we assume here that file has no EXIF, so we create new.
    ExifData* exif = exif_data_new();
    exif_data_set_option(exif, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
    exif_data_set_data_type(exif, EXIF_DATA_TYPE_COMPRESSED);
    exif_data_set_byte_order(exif, EXIF_BYTE_ORDER_INTEL);
    exif_data_fix(exif);

    // Create entry for "Software" and set it's data.
    ExifEntry* entry = create_ascii_tag(exif, EXIF_IFD_0, EXIF_TAG_SOFTWARE, strlen(tagvalue) + 1);
    memcpy(entry->data, tagvalue, strlen(tagvalue) + 1);

    // Write exif to JPEG.
    JPEGData* image = jpeg_data_new_from_file(filename);
    jpeg_data_set_exif_data(image, exif);
    jpeg_data_save_file(image, filename);

    // Unreference exif.
    exif_data_unref(exif);
    return 0;
}


int main(int argc, char ** argv) {
    char* filename = argv[1];

    // Create EXIF, set software and write EXIF to file.
    create_exif_and_set_software(filename, "Awesome image editor.");

    // Check that EXIF exists in file.
    ExifData* exif = exif_data_new_from_file(filename);
    printf("EXIF exists: %d\n", exif != NULL);

    return 0;
}

你可以在这里下载project and sample images。我使用以下命令在mac上构建它,并通过macports安装libexif : gcc exif.c -I/opt/local/include -L/opt/local/lib -lexif

EN

回答 1

Stack Overflow用户

发布于 2014-03-19 19:36:52

最简单的方法是从头开始编写JPEG :首先,写入没有APP1节的JPEG,然后添加Exif节,然后添加所有其他的XMP APP1节。

请注意,IFD偏移量应适当更新。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17019476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档