I'm trying to convert an array of integers to a PNG-file.
function saveImg(data, dir, name, waveBand) {
if (!fs.existsSync(dir)){
logger.info(dir + ' does not exist... creating!');
fs.mkdirSync(dir);
}
logger.info('received data with length ' + data.length);
try {
const encoder = new util.TextDecoder("utf-8");
const decodedData = encoder.decode(new Uint8Array(data));
logger.info('decoded string: ' + decodedData);
const imgLocation = path.join(getStoragePublicFolder(), dir, name + '.png');
logger.info('Writing image to location: ' + imgLocation);
fs.writeFile(path.resolve(imgLocation), decodedData,
function(err) {
if(err)
logger.info(err);
}
);
logger.info('Success! Image saved to ' + imgLocation);
return imgLocation;
}
catch (err)
{
logger.error('Failed to save image - ' + err);
}
return defaultPath(waveBand);
}
My data looks like this: https://jsfiddle.net/ebaxg3q4/
Unfortunately there is something wrong with the decoding of the data therefore the image is corrupted.
Which kind of encoding is this? What is the error I made?
Thank you in advance!