1

I wrote an Android app to send some text (which should be saved in a file) and an image (which should be saved as a seperate file) with a HTTP-POST-request using the volley lib.

The image is converted to a byte array. This byte array is converted to a string using Base64.encodeToString(). This string is set as attribute in an JSON-object which is send to the server.

The interface on the server-side is a node-red-server: The data is received and the file names and the text are extracted. So far everything works fine. But when saving the image (which is a Base64-encoded string) only writes the string in a jpg-file.

What's left to do is decode the image string and save it as image. I don't know how to do this. I tried this approach, but node-red does not know Image().

Also I did not fully understand, why the best way to send an image is to parse it to a byte-array and encode this array as Base64-string. If someone could explain the idea or link me a tutorial this might help me.

Please let me know if you need my app-code/node-red-flows to help.

hardillb
  • 54,545
  • 11
  • 67
  • 105
liquid.pizza
  • 505
  • 1
  • 10
  • 26

2 Answers2

2

There is a base64 Node-RED node (node-red-node-base64) that will convert a base64 encoded string into a NodeJS binary buffer.

If you run your string through this before sending it to a file node you will get a correctly formatted image.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • thanks for the fast answer. I tried this node, but I still can't open the images. I pass the image-string as payload to this node. the resulting payload should be the content of the jpg-file. Did I understand that correct? – liquid.pizza Sep 06 '17 at 12:12
  • I tried to open the files and my programm told me "Error interpreting JPEG image file (Not a JPEG file: starts with 0x4c 0x7a)". If the image I capture is a jpg and I encode it and send it to the server it should still be a jpg, shouldn't it? I got this error with two different images – liquid.pizza Sep 06 '17 at 12:21
  • Assuming what you encoded on the Android side is a properly formatted jpeg stream then this should do what you need. – hardillb Sep 06 '17 at 12:29
  • after encoding the image to a string I just decoded it again and displayed it in an image view. The encoding seems to be correct. – liquid.pizza Sep 06 '17 at 13:43
  • I solved this. thanks for your help, but it is important that the node takes a single line string as input! – liquid.pizza Sep 07 '17 at 08:09
0

I solved this. For everyone else who is looking for an answer I post this:

The base64 Node-RED node hardillb mentioned in his answer works fine, but only if the whole string is in one line.

If you use the encodeToString() function with the Base64.DEFAULT flag like this: String returnString = Base64.encodeToString(bytes, Base64.DEFAULT); Your recieve a multi line string. If you use the flag Base64.NO_WRAP instead it works fine with the node.

liquid.pizza
  • 505
  • 1
  • 10
  • 26