6

How do I convert a byte array to an image in Node.js? I have tried the code below. It creates an image but when I open it, I get an error message

Error interpreting JPEG image file.

I have converted the byte array from bitmap in Android and sent it to a Node server.

var mkdirp = require('mkdirp');

var fs = require('fs');

var bitmap = new Buffer(bytearray,'base64');

mkdirp(__dirname + "/images/",function(er)
{

  if(er) console.log(er);

  fs.writeFileSync(__dirname + "/images/"+profile._id+".jpeg",bitmap);

});
Jawa
  • 2,328
  • 6
  • 34
  • 39
scionoftech
  • 588
  • 1
  • 7
  • 23
  • Are you trying to store a bitmap as .jpg? shouldn't it be bmp? – Manuel Miranda Sep 10 '15 at 09:34
  • at android side, have you created base64 string of image? – Hiren S. Sep 10 '15 at 10:10
  • thank you for reply,i have created base64 string of image in android and send to node server,it created image successfully but, it not looks like how i sent,height and width of image has changed. – scionoftech Sep 10 '15 at 10:55
  • this might help: https://stackoverflow.com/questions/40376410/byte-array-into-an-image-node-js – Sumeet Basak Sep 27 '17 at 12:35
  • [this answer](https://stackoverflow.com/questions/37608249/convert-base64-image-to-a-file-in-node-js?answertab=oldest#tab-top) worked fine for me. – Amor Nov 19 '17 at 11:23

0 Answers0