NodeJS:使用PIEXIF更新Exif数据并保存图像

我需要更新上传图像的方向标签(EXIF数据)。 我正在使用“PIEXIF” 。 我不是用快递,而是大肆吹嘘。 我写的代码是:

//Get the uploaded buffer var _originalBuffer = req.swagger.params.uploadedFile.value.buffer; let Duplex = require('stream').Duplex; //Create stream from buffer. This stream is required later to send to cloud. let _uploadedFileStream = new Duplex(); _uploadedFileStream.push(_originalBuffer); _uploadedFileStream.push(null); //Create base 64 string so that "PIEXIF" can read exif data from it. const jpegData = "data:image/jpeg;base64, " + createStringFromBuffer(_originalBuffer, 'base64'); //Read exif data. var _exifData = piexif.load(jpegData); //Create a copy of exif data. Will be used to create a new image with updated orientation tag. var _exifDataCopied = {}; for (var key in _exifData) { _exifDataCopied[key] = _exifData[key]; } //Update orientation tag. if (_exifDataCopied["0th"][piexif.ImageIFD.Orientation]) _exifDataCopied["0th"][piexif.ImageIFD.Orientation] = 1; //Example taken from https://www.npmjs.com/package/piexifjs //From here onwards, there seems to be an issue. var exifbytes = piexif.dump(_exifDataCopied); var newData = piexif.insert(exifbytes, createStringFromBuffer(_originalBuffer, 'binary')); var newJpeg = new Buffer(newData); //Create a new stream and save it as image back. let _updatedFileStream = new Duplex(); _updatedFileStream.push(newJpeg); _updatedFileStream.push(null); var fs = require('fs'); var writeStream = fs.createWriteStream("./uploads/" + "Whatever.jpg") 

问题是代码没有引发错误。 图像也被保存在目录中,但已损坏。 我无法预览它。 由于代码不会在任何地方被打破,我很困惑可能是什么问题? 将缓冲区转换为具有不同编码的string的函数(因为我需要它)是:

 var createStringFromBuffer = function(_buffer, _encoding) { return Buffer.from(_buffer).toString(_encoding); } 

有人能指出我错在哪里吗? 我正在使用这里给出的例子