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