# BIN Image from device

**URL:** <https://discourse.nodered.org/t/bin-image-from-device/82509>\
**Category:** Industrial\
**Created:** [1 November 2023 07:56 UTC](https://discourse.nodered.org/t/bin-image-from-device/82509 "2023-11-01T07:56:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![schnello](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/schnello/32/65802_2.png) [@schnello](https://discourse.nodered.org/u/schnello)\
**Post date:** [1 November 2023 07:56 UTC](https://discourse.nodered.org/t/bin-image-from-device/82509/1 "2023-11-01T07:56:43Z")

</div>

Hello.

We use node-red to controll a Vision Inspection System. Works very well but one thing i miss.

I want get directly via TCP the image from the camera.  
From the inspection system manual:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3d7ccc466c90ed1115aa6f6bee672d5714c8fe01.png)

I demo code written in C:

> <https://gist.github.com/schnello/815085ad13457da7b6f3f17a46e96d78>

Any ideas how I could achieve that?

Thanks,  
Christian

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [1 November 2023 08:26 UTC](https://discourse.nodered.org/t/bin-image-from-device/82509/2 "2023-11-01T08:26:00Z")

</div>

I'll have a go at this shortly - if no one else jumps in (just about to do school run)

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [1 November 2023 10:08 UTC](https://discourse.nodered.org/t/bin-image-from-device/82509/3 "2023-11-01T10:08:08Z")

</div>

@schnello

Reading the c# code, this will get you _close_, it will likely need some tweaking, and you will need to install the buffer parser node (this is my first time using it 😄)

> **[node-red-contrib-buffer-parser](https://flows.nodered.org/node/node-red-contrib-buffer-parser)**
>
> Node-red nodes to convert values to and from buffer/array. Supports Big/Little Endian, BCD, byte swapping and much more

From what you have pasted - it uses ascii to detail the size of the image (which is a shame) - unless there is another way in the parser (@Steve-Mcl?)

Anyway, it should get you fairly close.

You may need to also install

> **[node-red-contrib-image-tools](https://flows.nodered.org/node/node-red-contrib-image-tools)**
>
> A dynamic image editor, image viewer, 1D/2D Barcode Generator and 1D/2D Barcode Decoder nodes

To utilise the pixel data.  
You will need to also provide the IP/PORT of the server.

Have fun!

```auto
[{"id":"e218090699024d27","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"c266203814a68d6b","type":"tcp request","z":"e218090699024d27","name":"","server":"","port":"","out":"sit","ret":"buffer","splitc":" ","newline":"","trim":false,"tls":"","x":630,"y":185,"wires":[["62fa6fd384fd99c5"]]},{"id":"710de31313433d77","type":"inject","z":"e218090699024d27","name":"Trigger","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":155,"y":185,"wires":[["4f2ffde5f595ccad"]]},{"id":"4f2ffde5f595ccad","type":"change","z":"e218090699024d27","name":"Trigger (TRG)","rules":[{"t":"set","p":"expectImage","pt":"flow","to":"false","tot":"bool"},{"t":"set","p":"payload","pt":"msg","to":"[84,82,71]","tot":"bin"}],"action":"","property":"","from":"","to":"","reg":false,"x":375,"y":185,"wires":[["c266203814a68d6b"]]},{"id":"3f646f5147769daa","type":"change","z":"e218090699024d27","name":"Request (Last Good GIM2)","rules":[{"t":"set","p":"expectImage","pt":"flow","to":"true","tot":"bool"},{"t":"set","p":"payload","pt":"msg","to":"[71,73,77,49]","tot":"bin"}],"action":"","property":"","from":"","to":"","reg":false,"x":425,"y":235,"wires":[["c266203814a68d6b"]]},{"id":"052f8f2467205cd4","type":"link out","z":"e218090699024d27","name":"Request Image","mode":"link","links":["514c2a6dc4aa3735"],"x":1030,"y":205,"wires":[],"inputLabels":["Request"],"l":true},{"id":"514c2a6dc4aa3735","type":"link in","z":"e218090699024d27","name":"Request Image","links":["052f8f2467205cd4"],"x":160,"y":235,"wires":[["3f646f5147769daa"]],"l":true},{"id":"62fa6fd384fd99c5","type":"switch","z":"e218090699024d27","name":"Expext Image?","property":"expectImage","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":805,"y":185,"wires":[["7e24bb14c686588c"],["052f8f2467205cd4"]]},{"id":"7e24bb14c686588c","type":"link out","z":"e218090699024d27","name":"Process Image","mode":"link","links":["3651691b95fc244d"],"x":1030,"y":165,"wires":[],"l":true},{"id":"3651691b95fc244d","type":"link in","z":"e218090699024d27","name":"Prcoess Image","links":["7e24bb14c686588c"],"x":160,"y":370,"wires":[["825bd2749d69c9ee"]],"l":true},{"id":"ff14af022f8808cb","type":"inject","z":"e218090699024d27","name":"Test Data (10*10) size","props":[{"p":"payload"},{"p":"JustSoWeHaveACopy","v":"GIMP001001000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[71,73,77,80,48,48,49,48,48,49,48,48,48,49,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48,48]","payloadType":"bin","x":195,"y":440,"wires":[["825bd2749d69c9ee"]]},{"id":"825bd2749d69c9ee","type":"buffer-parser","z":"e218090699024d27","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"ascii","name":"passFail","offset":3,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"ascii","name":"errorCode","offset":4,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"ascii","name":"imageType","offset":5,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"ascii","name":"imageResult","offset":6,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"ascii","name":"rows","offset":7,"length":4,"offsetbit":0,"scale":"1","mask":""},{"type":"ascii","name":"cols","offset":11,"length":4,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":375,"y":370,"wires":[["f341d24989f463da"]]},{"id":"f341d24989f463da","type":"change","z":"e218090699024d27","name":"Parse Interger ASCII","rules":[{"t":"set","p":"payload.rows","pt":"msg","to":"$number(payload.rows)","tot":"jsonata"},{"t":"set","p":"payload.cols","pt":"msg","to":"$number(payload.cols)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":605,"y":370,"wires":[["2a1702abdb5c5619"]]},{"id":"2a1702abdb5c5619","type":"function","z":"e218090699024d27","name":"Extract Image Data","func":"const imageData = msg.buffer.slice(15,msg.payload.rows*msg.payload.cols)\nmsg.payload.imageData = imageData\nreturn msg","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":845,"y":370,"wires":[["0da54066cf789b20"]]},{"id":"0da54066cf789b20","type":"debug","z":"e218090699024d27","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1055,"y":365,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![schnello](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/schnello/32/65802_2.png) [@schnello](https://discourse.nodered.org/u/schnello)\
**Post date:** [1 November 2023 11:51 UTC](https://discourse.nodered.org/t/bin-image-from-device/82509/4 "2023-11-01T11:51:09Z")

</div>

Hey.

First: Thanks for your help. I would be happy if I could really crack the nut with help.  
I try the flow and i got the image data as a buffer. But i cant read or display the image.  
Error Message from the image node:  
Error: Could not find MIME for Buffer

As i understand the flow does extract the image data with msg.buffer.slice(15,msg.payload.rows\*msg.payload.cols)  
This is the same as the c code does.

But what i guess what is missing is the creation of the image.  
drawBmp(imgStream, col, row);

I dont know what drawBmp is. It looks like this function create with the buffer + row count + col count the final image.

Edit: Found this one. Will try if i can create a image with this informations.

> <https://gist.github.com/siberex/9b843d5f3b0c573f6d3c4f4ca7205f73>

Thanks,  
Christian

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [1 November 2023 12:09 UTC](https://discourse.nodered.org/t/bin-image-from-device/82509/5 "2023-11-01T12:09:29Z")

</div>

That code should pack the pixel data into a correct BIMTAP file...  
you should in theory be able to then pass the result into the image view.

Sadly, I'm not an image file / specification expert 😆

**EDIT**  
Raw Pixel data isn't enough sometimes.  
The rows, cols may only specify the size of the blob - but you also need the `stride`

- How many cols, make up a scanline in the image

In .NET this is why it needs to know the pixel format - in this case it appears to be `8bppIndexed`  
Else you will get an image that skews for each scanline and will appear leaning towards one side

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [31 December 2023 12:10 UTC](https://discourse.nodered.org/t/bin-image-from-device/82509/6 "2023-12-31T12:10:25Z")

</div>

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