- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 libexif 将 exif 数据写入 jpeg 文件。我非常关注随附的示例。我可以理解所有正在写入的 Exif 数据 block 和所有内容,但是如何按照他们的建议加载 jpeg 数据? (使用 libjpeg)。我找不到可以直接让我将 jpeg 导入为 char 的函数。如果我包含一个虚拟 jpg,我的代码可以正常工作,如下所示。
/*
* 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>
/* this file will be unilaterally overwritten */
#define FILE_NAME "write-exif.jpg"
/* raw JPEG image data */
static const unsigned char image_jpg[] = {
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43,
0x00, 0x14, 0x0e, 0x0f, 0x12, 0x0f, 0x0d, 0x14, 0x12, 0x10, 0x12, 0x17,
0x15, 0x14, 0x18, 0x1e, 0x32, 0x21, 0x1e, 0x1c, 0x1c, 0x1e, 0x3d, 0x2c,
0x2e, 0x24, 0x32, 0x49, 0x40, 0x4c, 0x4b, 0x47, 0x40, 0x46, 0x45, 0x50,
0x5a, 0x73, 0x62, 0x50, 0x55, 0x6d, 0x56, 0x45, 0x46, 0x64, 0x88, 0x65,
0x6d, 0x77, 0x7b, 0x81, 0x82, 0x81, 0x4e, 0x60, 0x8d, 0x97, 0x8c, 0x7d,
0x96, 0x73, 0x7e, 0x81, 0x7c, 0xff, 0xc0, 0x00, 0x0b, 0x08, 0x00, 0x40,
0x00, 0x40, 0x01, 0x01, 0x11, 0x00, 0xff, 0xc4, 0x00, 0x1b, 0x00, 0x00,
0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x04, 0x03, 0x07, 0x02, 0x01, 0xff,
0xc4, 0x00, 0x2f, 0x10, 0x00, 0x01, 0x03, 0x03, 0x02, 0x05, 0x03, 0x03,
0x03, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x11,
0x00, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x81, 0x61, 0x71,
0x91, 0x13, 0x32, 0xa1, 0x14, 0x22, 0xc1, 0x15, 0x23, 0x52, 0xd1, 0xf0,
0xff, 0xda, 0x00, 0x08, 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00, 0xb3, 0xa2,
0x8a, 0x28, 0xa2, 0x8a, 0x28, 0xa2, 0x97, 0x64, 0x72, 0xd6, 0x58, 0xd4,
0x8f, 0xd5, 0x3d, 0xca, 0xa5, 0x7d, 0xa8, 0x4e, 0xaa, 0x3e, 0xb0, 0x3a,
0x7a, 0x9d, 0x2b, 0x2d, 0x87, 0x13, 0xe3, 0xaf, 0x9f, 0x0c, 0xb6, 0xb5,
0xb6, 0xe2, 0xb4, 0x48, 0x71, 0x30, 0x14, 0x7b, 0x02, 0x09, 0x14, 0xee,
0x94, 0x64, 0x38, 0x87, 0x1f, 0x8d, 0x5f, 0xd3, 0x7d, 0xe2, 0xa7, 0x46,
0xe8, 0x6c, 0x49, 0x1e, 0xfd, 0x07, 0xb1, 0x33, 0x5f, 0xb8, 0xdc, 0xf5,
0x96, 0x51, 0x7c, 0x96, 0xee, 0x14, 0xba, 0x04, 0xf2, 0x2c, 0x42, 0xa3,
0xd3, 0x70, 0x7c, 0x1a, 0x6d, 0x45, 0x2c, 0xcd, 0xe5, 0x11, 0x89, 0xb1,
0x5b, 0xc4, 0x02, 0xe1, 0xd1, 0xb4, 0xcf, 0xdc, 0xa3, 0xfc, 0x0d, 0xcd,
0x41, 0xde, 0xe3, 0xb2, 0x2e, 0xda, 0x1c, 0xbd, 0xe2, 0x4a, 0x90, 0xe9,
0x04, 0x95, 0x2b, 0xf7, 0x41, 0xd0, 0x18, 0xe8, 0x36, 0x03, 0xc6, 0x91,
0x4a, 0x81, 0x20, 0x82, 0x24, 0x10, 0x66, 0x45, 0x5a, 0xdd, 0xf1, 0x32,
0xff, 0x00, 0xa2, 0x5a, 0x8b, 0x62, 0x57, 0x7d, 0x70, 0x80, 0x93, 0x1a,
0x94, 0x10, 0x79, 0x4a,
};
/* length of data in image_jpg */
static const unsigned int image_jpg_len = sizeof(image_jpg);
/* dimensions of image */
static const unsigned int image_jpg_x = 64;
static const unsigned int image_jpg_y = 64;
/* start of JPEG image data section */
static const unsigned int image_data_offset = 20;
#define image_data_len (image_jpg_len - image_data_offset)
/* raw EXIF header data */
static const unsigned char exif_header[] = {
0xff, 0xd8, 0xff, 0xe1
};
/* length of data in exif_header */
static const unsigned int exif_header_len = sizeof(exif_header);
/* 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"
/* Get an existing tag, or create one if it doesn't exist */
static ExifEntry *init_tag(ExifData *exif, ExifIfd ifd, ExifTag tag)
{
ExifEntry *entry;
/* Return an existing tag if one exists */
if (!((entry = exif_content_get_entry (exif->ifd[ifd], tag)))) {
/* Allocate a new entry */
entry = exif_entry_new ();
assert(entry != NULL); /* catch an out of memory condition */
entry->tag = tag; /* tag must be set before calling
exif_content_add_entry */
/* Attach the ExifEntry to an IFD */
exif_content_add_entry (exif->ifd[ifd], entry);
/* Allocate memory for the entry and fill with default data */
exif_entry_initialize (entry, tag);
/* Ownership of the ExifEntry has now been passed to the IFD.
* One must be very careful in accessing a structure after
* unref'ing it; in this case, we know "entry" won't be freed
* because the reference count was bumped when it was added to
* the IFD.
*/
exif_entry_unref(entry);
}
return entry;
}
/* Create a brand-new tag with a data field of the given length, in the
* given IFD. This is needed when exif_entry_initialize() isn't able to create
* this type of tag itself, or the default data length it creates isn't the
* correct length.
*/
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 = 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)
{
int rc = 1;
FILE *f;
unsigned char *exif_data;
unsigned int exif_data_len;
ExifEntry *entry;
ExifData *exif = exif_data_new();
if (!exif) {
fprintf(stderr, "Out of memory\n");
return 2;
}
/* Set the image options */
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, FILE_BYTE_ORDER);
/* Create the mandatory EXIF fields with default data */
exif_data_fix(exif);
/* All these tags are created with default values by exif_data_fix() */
/* Change the data to the correct values for this image. */
entry = init_tag(exif, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION);
exif_set_long(entry->data, FILE_BYTE_ORDER, image_jpg_x);
entry = init_tag(exif, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION);
exif_set_long(entry->data, FILE_BYTE_ORDER, image_jpg_y);
entry = init_tag(exif, EXIF_IFD_EXIF, EXIF_TAG_COLOR_SPACE);
exif_set_short(entry->data, FILE_BYTE_ORDER, 1);
/* Create a EXIF_TAG_USER_COMMENT tag. This one must be handled
* differently because that tag isn't automatically created and
* allocated by exif_data_fix(), nor can it be created using
* exif_entry_initialize() so it must be explicitly allocated here.
*/
entry = create_tag(exif, 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(exif, EXIF_IFD_EXIF, EXIF_TAG_SUBJECT_AREA,
4 * exif_format_get_size(EXIF_FORMAT_SHORT));
entry->format = EXIF_FORMAT_SHORT;
entry->components = 4;
exif_set_short(entry->data, FILE_BYTE_ORDER, image_jpg_x / 2);
exif_set_short(entry->data+2, FILE_BYTE_ORDER, image_jpg_y / 2);
exif_set_short(entry->data+4, FILE_BYTE_ORDER, image_jpg_x);
exif_set_short(entry->data+6, FILE_BYTE_ORDER, image_jpg_y);
/* Get a pointer to the EXIF data block we just created */
exif_data_save_data(exif, &exif_data, &exif_data_len);
assert(exif_data != NULL);
f = fopen(FILE_NAME, "wb");
if (!f) {
fprintf(stderr, "Error creating file %s\n", FILE_NAME);
exif_data_unref(exif);
return rc;
}
/* Write EXIF header */
if (fwrite(exif_header, exif_header_len, 1, f) != 1) {
fprintf(stderr, "Error writing to file %s\n", FILE_NAME);
goto errout;
}
/* Write EXIF block length in big-endian order */
if (fputc((exif_data_len+2) >> 8, f) < 0) {
fprintf(stderr, "Error writing to file %s\n", FILE_NAME);
goto errout;
}
if (fputc((exif_data_len+2) & 0xff, f) < 0) {
fprintf(stderr, "Error writing to file %s\n", FILE_NAME);
goto errout;
}
/* Write EXIF data block */
if (fwrite(exif_data, exif_data_len, 1, f) != 1) {
fprintf(stderr, "Error writing to file %s\n", FILE_NAME);
goto errout;
}
/* 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;
}
printf("Wrote file %s\n", FILE_NAME);
rc = 0;
errout:
if (fclose(f)) {
fprintf(stderr, "Error writing to file %s\n", FILE_NAME);
rc = 1;
}
/* The allocator we're using for ExifData is the standard one, so use
* it directly to free this pointer.
*/
free(exif_data);
exif_data_unref(exif);
return rc;
}
最佳答案
您可以只使用 fopen 函数来加载特定的 jpg。然后将exif数据写入其中。
关于c - 使用 libexif 将 exif 数据写入 jpg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24685839/
我正在使用 exiv2操作 jpeg 文件中的元数据。我需要将更多与图像处理相关的信息写入元数据中。除了标准标签之外,是否可以创建自定义 Exif 标签? 最佳答案 来自 http://www.exi
是否可以检测是否有人修改了照片中的 EXIF 数据?有人如何检测到 EXIF 数据已被修改? 最佳答案 参见 Can digital cameras sign images to prove auth
如上图所示,我选择拍照exif信息为空。我选择了来自手机的图像选项,但 exif 信息不为空。 最佳答案 现在可以通过 ExifInterface 支持库从流中读取 EXIF 数据。 compile
我想使用iphone-exif library我的项目中的 EXIF 数据。但我不知道我是否犯了任何错误,它给出了像 这样的错误 架构 i386 的 undefined symbol : “_OBJC
场景:几个人带着数码相机一起去度假,然后抓拍。有些人记得将他们的相机时钟调整为本地时间,有些人将它们留在家乡时间,有些人将它们留在他们出生国家的本地时间,还有一些人将他们的相机留在工厂时间。 问题:照
如何加载图像并根据它的方向 exif 数据旋转它并使用UIImageOrientationUp exif 数据(或没有任何方向)保存它exif 数据)以便不处理 exif 方向数据的软件将正确显示图像
PHP 版本 5.2.9 我想知道是否有人在使用 EXIF 2.3 的 PHP 的 exif_read_data() 提取 GPS 或什至只是提取所有 EXIF 数据时遇到问题(并可能找到解决方案)。
我试图了解 jpeg 文件(十六进制)的 EXIF header 部分以及如何理解它,以便我可以提取数据,特别是 GPS 信息。无论好坏,我都在使用 VB.Net 2008(对不起,这是我现在可以掌握
我喜欢遵循此处描述的简码约定:https://laurakalbag.com/processing-responsive-images-with-hugo/并像这样在 config.toml 中设置
有谁知道 EXIF 键名的一个很好的解释?我正在写一个照片组织者,并希望尽可能多地从照片中获取信息。 但是,EXIF 键名并不是很有帮助。例如,据我所知(通过从 iPhoto 导出图像) 专辑或集合名
我正在研究照片查看器。在这种情况下,我编写了一个小类来读取和使用一些 EXIF 数据,例如图像方向。这门课很适合阅读。 但是,我会添加一个新选项来旋转照片。我想旋转和写入照片数据本身,而不仅仅是重写方
我是 PHP 新手,正在改编来自 http://www.techrepublic.com/article/create-a-dynamic-photo-gallery-with-php-in-thre
我正在使用我正在拍摄的照片中的 iPhone EXIF 数据。 目前我得到的 EXIF 数据是: { ApertureValue = "2.970854"; ColorSpace =
我有一堆带有地理标记的图片,我正在使用 pyexiv2 在 python 中访问它们。它工作得很好,只是我不明白“GPSImgDirection”值给出了什么。基本上,它是两个很大的数字,例如: 21
在Python中,如何找出图像文件中EXIF标签的位置?我假设它位于文件的开头,但我有一些专有的图像格式,并且希望确保情况始终如此。谢谢。 最佳答案 这不仅取决于 EXIF 本身,还取决于文件类型。
我正在尝试从 DNG 文件(不是缩略图)获取 jpg 预览。据我所知,预览只是一个 exif 预览,并不特定于 DNG。然而,尽管我在谷歌上搜索了很多次,但我找不到任何关于专门从 c# 文件的 exi
我正在使用这个库 http://blog.nihilogic.dk/2008/05/reading-exif-data-with-javascript.html .我这样调用库:EXIF.pretty
我需要访问已加载到页面上的图像中的 EXIF 数据。说,从浏览器扩展。 AFAIU,有一些 javascript 方法可以完成任务: 使用JavaScript-Load-Image ; 使用Nihol
我有一个 iOS 应用程序,用户可以在其中上传使用 UIImagePickerController 拍摄的照片。 当我查看 jpg exif 数据时,我注意到大量照片的“相机型号名称”中填写了“iPo
我想在特定相册中保存一些带有图像的字符串。所以我使用了[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{添加 Assets 。 但是当我从相册
我是一名优秀的程序员,十分优秀!