簡體   English   中英

在 javascript 中使用字節 (UCS2) 類型修改 jpg 圖像 Exif

[英]Modify jpg image Exif with Byte (UCS2) Type in javascript

我正在嘗試使用piexifjs模塊修改 jpg 的 Exif 數據。 它運作良好,但對於最重要和最基本的標簽,如TitleCommentAuthor ......它根本不起作用。

var piexif = require("piexifjs");
var fs = require("fs");

var filename1 = "image.jpg";
var filename2 = "out.jpg";

var jpeg = fs.readFileSync(filename1);
var data = jpeg.toString("binary");

var zeroth = {};

zeroth[piexif.ImageIFD.XPTitle] = "Birds";

var exifObj = {"0th":zeroth};
var exifbytes = piexif.dump(exifObj);

var newData = piexif.insert(exifbytes, data);
var newJpeg = Buffer.from(newData, "binary");
fs.writeFileSync(filename2, newJpeg);

當我運行它時,它會拋出Error: 'pack' error. Got invalid type argument. Error: 'pack' error. Got invalid type argument.
我在互聯網上搜索並在Metadata reference tables中說它們的類型為Byte https://www.exiv2.org/tags.html 所以當我把它zeroth[piexif.ImageIFD.XPTitle] = 4; 或它運行的某個數字,但不更改標題。
那么我怎樣才能讓它工作呢? 當他們的類型是Byte是什么意思? 怎么能

我在這里找到了答案。 你需要:

zeroth[piexif.ImageIFD.XPTitle]   = [...Buffer.from('Birds', 'ucs2')];
zeroth[piexif.ImageIFD.XPComment] = [...Buffer.from('Tweet tweet', 'ucs2')];
zeroth[piexif.ImageIFD.XPAuthor]  = [...Buffer.from('Ornithologist', 'ucs2')];

結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM