# Server that gets image stream from spout and send it to client

**URL:** <https://discourse.processing.org/t/server-that-gets-image-stream-from-spout-and-send-it-to-client/25563>\
**Category:** Libraries\
**Created:** [November 20, 2020, 2:44pm UTC](https://discourse.processing.org/t/server-that-gets-image-stream-from-spout-and-send-it-to-client/25563 "2020-11-20T14:44:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phaidon](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@phaidon](https://discourse.processing.org/u/phaidon)\
**Post date:** [November 20, 2020, 2:44pm UTC](https://discourse.processing.org/t/server-that-gets-image-stream-from-spout-and-send-it-to-client/25563/1 "2020-11-20T14:44:50Z")

</div>

hello all, i try to get a texture from spout and send it thought server and client, the data flow seems working but i can reconvert the bytes into an image

the scripts are here

server with spout:

> import java.nio.ByteBuffer;  
> import spout._;  
> import [processing.net](http://processing.net)._;
> 
> Server s;  
> Client c;  
> String input;  
> int data;  
> PGraphics pgr; // Canvas to receive a texture  
> PImage img; // Image to receive a texture
> 
> Spout spout;
> 
> void setup() {
> 
> // Initial window size  
> size(640, 360, P3D);
> 
> surface.setResizable(true);
> 
> // Create a canvas or an image to receive the data.  
> pgr = createGraphics(width, height, PConstants.P2D);  
> img = createImage(width, height, ARGB);
> 
> // Graphics and image objects can be created  
> // at any size, but their dimensions are changed  
> // to match the sender that the receiver connects to.
> 
> // CREATE A NEW SPOUT OBJECT  
> spout = new Spout(this);  
> s = new Server(this, 12345);  
> // OPTION : CREATE A NAMED SPOUT RECEIVER  
> //  
> // By default, the active sender will be detected  
> // when receiveTexture is called. But you can specify  
> // the name of the sender to initially connect to.  
> // spout.createReceiver(“Spout DX11 Sender”);  
> }
> 
> void draw() {
> 
> background(0);
> 
> //  
> // RECEIVE A SHARED TEXTURE  
> //
> 
> // OPTION 1: Receive and draw the texture  
> [//spout.receiveTexture](https://spout.receiveTexture)();
> 
> // OPTION 2: Receive into PGraphics texture  
> // pgr = spout.receiveTexture(pgr);  
> // image(pgr, 0, 0, width, height);
> 
> // OPTION 3: Receive into PImage texture  
> img = spout.receiveTexture(img);  
> image(img, 0, 0, width, height);
> 
> byte array = convertImageToByteArray(img);  
> s.write(array);  
> print(array);  
> // OPTION 4: Receive into PImage pixels  
> // img = spout.receivePixels(img);  
> // image(img, 0, 0, width, height);
> 
> // Optionally resize the window to match the sender  
> // spout.resizeFrame();  
> }
> 
> // SELECT A SPOUT SENDER  
> void mousePressed() {  
> // RH click to select a sender  
> if (mouseButton == RIGHT) {  
> // Bring up a dialog to select a sender.  
> // Spout installation required  
> spout.selectSender();  
> }  
> }
> 
> byte convertImageToByteArray(PImage img) {  
> img.loadPixels();  
> int px = img.pixels;  
> ByteBuffer bb = ByteBuffer.allocate((2 + px.length) \* 4);  
> bb.putInt(img.width);  
> bb.putInt(img.height);  
> for (int d : px) {  
> bb.putInt(d);  
> }  
> return bb.array();  
> }

client to receive

> import [processing.net](http://processing.net).\*;  
> import java.nio.ByteBuffer;  
> Client c;
> 
> int data;
> 
> void setup() {  
> size(640, 360,P3D);
> 
> // Slow it down a little  
> // Connect to the server’s IP address and port­  
> c = new Client(this, “127.0.0.1”, 12345); // Replace with your server’s IP and port  
> }
> 
> void draw() {
> 
> // Receive data from server  
> if (c.available() \> 0) {  
> byte input = c.readBytes();  
> ////input = input.substring(0, input.indexOf(“\n”)); // Only up to the newline  
> //input = input.substring(0,input.indexOf(“\n”)); // Only up to the newline  
> //data = int(split(input, ’ '));  
> // Split values into an array
> 
> ```
> PImage imgNew = convertByteArrayToImage(input);
> //text("Byte array > image", 220, 16);
> image(imgNew, 0, 0);
> 
> ```
> 
> }  
> }
> 
> PImage convertByteArrayToImage(byte bytes) {  
> ByteBuffer bb = ByteBuffer.wrap(bytes);  
> int w = bb.getInt();  
> int h = bb.getInt();  
> PImage img = new PImage(w, h);  
> int px = img.pixels;  
> for (int i = 0; i \< px.length; i++) {  
> px[i] = bb.getInt();  
> }  
> img.updatePixels();  
> return img;  
> }

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [November 20, 2020, 6:04pm UTC](https://discourse.processing.org/t/server-that-gets-image-stream-from-spout-and-send-it-to-client/25563/2 "2020-11-20T18:04:10Z")

</div>

I haven’t run your code but I’m not sure if you update the `img.pixels()` array at the very bottom, inside your `convertByteArrayToImage()`.

I’m a bit rusty on java refs though so

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [November 20, 2020, 6:05pm UTC](https://discourse.processing.org/t/server-that-gets-image-stream-from-spout-and-send-it-to-client/25563/3 "2020-11-20T18:05:38Z")

</div>

Oh also, can you clarify: are you getting errors, runtime errors, a black screen? Whats the specific error?
