Convert buffer to uInt8array

Hi guys,

I am using node-red to implement an ap and I am racking my brains to figure out how to a buffer to uint8array (I am not sure that is that type).

The node "jimp-image" of module "node-red-contrib-image-tools" outputs an image as a buffer or a base64 string. I need to pass the responsed image into a web service which requires an uint8array base image.

Below is the output buffer format from jimp-image:

2

But I need the following type of buffer:

1

Do you have any idea how to convert the buffer to an array buffer as showed into the attached image?

Thank you very much for any support you can provide.

Instances of Buffer are also instances of Uint8Array in node.js 4.x and higher. Thus, the most efficient solution is to access the buf.buffer property directly, as per node.js - Convert a binary NodeJS Buffer to JavaScript ArrayBuffer - Stack Overflow

Hi Steve,
Thank you for your prompt reply.

Looking into the stackoverflow post you sent, what I want is exactly the opposite of this answer...

"From ArrayBuffer to Buffer" could be done this way:
var buffer = Buffer.from( new Uint8Array(ab) );

Do you have any idea how to do it, from Buffer to ArrayBuffer?

Thanks,

I dont know where you were reading?

demo...
image

try adding this to a function node after the image-tools output / before the http request...

let b = msg.payload; //get the image buffer
msg.payload = b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
return msg;

Hi Steve,

Sorry but this answer does not work.
It always returns an empty object not an array.

Am I doing something wrong?

Thank you for yor help.

No it's not. It's an ArrayBuffer. It states that in your screenshot. It's just that the debug output doesn't show you the contents like it does for a buffer.

Also, why have you used the hardcored buffer and not the payload from the image?

I have a feeling you don't know precisely what you are supposed to send to this API. It is quite possible a buffer sent with the correct headers etc will work perfectly fine.

Can you clarify that. Perhaps a link to documentation?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.