We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Sending pixles via Sever/Client
Page Index Toggle Pages: 1
Sending pixles via Sever/Client (Read 364 times)
Sending pixles via Sever/Client
Dec 8th, 2008, 5:52pm
 
I have a simple/server client set-up running on my computer, one sketch(server) sends out text and the other(client) receives it and displays it, my question is: how would I send a jpeg from one to the other?
thanks
Re: Sending pixles via Sever/Client
Reply #1 - Dec 26th, 2008, 2:49pm
 
hi

last year I coded a similar client server, though it transmits a screenshot, it can be adapted to transmit a jpeg (BufferedImage) as well.

Here is the code:

first the client:
Code:

/********************************
*
* Client um beim ScrennCaptureServer einem Bild
* anzufordern und direkt via netzwerk auf die
* eigene maschine zu laden.
*
*-------------------------------
*
* Autor Martin Fröhlich (2008
*
*********************************/


import java.io.*;
import java.net.*;

void setup(){
size(800, 800);
}

void keyPressed()
{
if(key == 's' || key == 'S') {
PImage img = getImageFromServer("maybites.local", 12001, (int)random(800), (int)random(800));
if(img != null){
image(img, 0, 0);
}
}
}

void draw(){
;
}

PImage getImageFromServer(String name, int port, int w, int h){
try {

InetAddress adress = InetAddress.getByName(name);
println("try contacting server on: " + adress.getHostAddress() + " port: " + port);
Socket socketConnection = new Socket(adress.getHostAddress(), port);

println("..success");

ObjectOutputStream clientOutputStream = new
ObjectOutputStream(socketConnection.getOutputStream());

ObjectInputStream clientInputStream = new
ObjectInputStream(socketConnection.getInputStream());

int[] dim = {w, h};


println("send request..");
clientOutputStream.writeObject(dim);
println("..sent");

println("waiting for result..");
int[] pxl = (int[])clientInputStream.readObject();

PImage img = new PImage(dim[0], dim[1]);
img.pixels = pxl;
img.updatePixels();

println("..received:" + pxl.length);


clientOutputStream.close();
clientInputStream.close();

socketConnection.close();

return img;

}
catch (UnknownHostException e) {
println("cannot find server: check host name for missspellings");
}
catch (Exception e) {
System.out.println(e);
}
return null;
}


and here the server:

Code:

/********************************
*
* Server um den ScrennCaptureClient mit einem Bild
* zu bedienen
*
*-------------------------------
*
* Autor Martin Fröhlich (2008
*
*********************************/

import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

int LISTENING_PORT = 12001;

Robot myrobie;

void setup()
{
myrobie = null;
try {
myrobie = new Robot();
}
catch( Exception e1 ) {
e1.printStackTrace();
}

serve();
}

BufferedImage captureImage(int x, int y, int w, int h){
Rectangle myScreen = new Rectangle(x, y, w, h);
BufferedImage myImage = myrobie.createScreenCapture(myScreen);
return myImage;
}

void serve(){

try {
ServerSocket socketConnection = new ServerSocket(LISTENING_PORT);

while (true) {

System.out.println("Server Waiting");
Socket pipe = socketConnection.accept();

System.out.println("Client Request received");

ObjectInputStream serverInputStream = new
ObjectInputStream(pipe.getInputStream());

ObjectOutputStream serverOutputStream = new
ObjectOutputStream(pipe.getOutputStream());

int[] req = (int[])serverInputStream.readObject();

System.out.println("Requested Image: " + req[0] + " x " + req[1]);

System.out.println("Create Screenshot...");

BufferedImage bimg = captureImage(0, 0, req[0], req[1]);
PImage img = new PImage(bimg);
img.loadPixels();
int[] pixel = img.pixels;

System.out.println("..done");

System.out.println("send image..");
serverOutputStream.writeObject(pixel);
System.out.println("..done");

serverInputStream.close();
serverOutputStream.close();
} // end of the while loop

}
catch(Exception e) {
System.out.println(e);
}
}


here some links:
http://java.sun.com/docs/books/tutorial/networking/sockets/readingWriting.html
http://www.developer.com/design/article.php/10925_3597071_1

it is possible to send complete objects this way (including objects that contain images) but I wasnt able to make that work, so I just send arrays. this has the obvious limitation that the receiver needs to now about the width and the height of the picture.

hope this helps

cheers

martin
Re: Sending pixles via Sever/Client
Reply #2 - Jan 14th, 2009, 5:03pm
 
wow t0gg1e4u,
thanks for sharing your code! when I start on this aspect of my project again this information will be greatly helpful

thanks again
Page Index Toggle Pages: 1