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);

});
5
  • Are you trying to store a bitmap as .jpg? shouldn't it be bmp? Commented Sep 10, 2015 at 9:34
  • at android side, have you created base64 string of image? Commented Sep 10, 2015 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. Commented Sep 10, 2015 at 10:55
  • this might help: stackoverflow.com/questions/40376410/… Commented Sep 27, 2017 at 12:35
  • this answer worked fine for me. Commented Nov 19, 2017 at 11:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.