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 › Send an image/video with Net library
Page Index Toggle Pages: 1
Send an image/video with Net library (Read 814 times)
Send an image/video with Net library
Nov 23rd, 2006, 2:25am
 
Hi all!

anyone knows how to send a compressed jpg with the Net library?

I've tried to send a byte array and everything works, but actually i'm sending uncompressed data...

/////////////////////////////////////////

SERVER code here:


import processing.net.*;

//Globals

Server s;

boolean doSend;

boolean alreadySent;

int numSends;

//Text

PFont font;

//Image
PImage capturedImg;

//Byte arrays for each image channel RGB
byte [] bR,bG,bB,b;

//

void setup(){

 size(200,200);

 doSend=false;

 alreadySent=false;

 numSends=0;

 s=new Server(this,5000);

 //Text
 font = loadFont("f.vlw");
 textFont(font, 8);
 fill(0,255,0);
 text("SERVER", 10, 15);

 //Image load
 
 capturedImg=loadImage("img.jpg");

 //Byte arrays for each image channel RGB
 bR=new byte[capturedImg.pixels.length];
 bG=new byte[capturedImg.pixels.length];
 bB=new byte[capturedImg.pixels.length];
 b=new byte[3*capturedImg.pixels.length];

 //Fill byte arrays
 for(int i=0;i<capturedImg.pixels.length;i++){
   
   //take RGB values of each pixel
   int r=int(red(capturedImg.pixels[i]));
   int g=int(green(capturedImg.pixels[i]));
   int b=int(blue(capturedImg.pixels[i]));
   
   //Put it in his array
   bR[i]=byte(r);
   bG[i]=byte(g);
   bB[i]=byte(b);
 }
 
 //Concat the arrays to get one single byte array to send over net
 byte [] bTemp=concat(bR,bG);
 b=concat(bTemp,bB);
 
 //Use byte array to show img
 for(int x=0;x<width;x++){
   for(int y=0;y<height;y++){
     int loc=x+y*width;
     // bytes are from -128 to 127, this converts to 0 to 255
     int _r = b[loc] & 0xff;
     int _g = b[capturedImg.pixels.length+loc] & 0xff;
     int _b = b[2*capturedImg.pixels.length+loc] & 0xff;
     //Set the color of each pixel in output
     color c= color(_r,_g,_b);
     set(x,y,c);
   }
 }

 println("Image from byte array showed");

}

void draw(){

 if(doSend==true){
   if(alreadySent==false){
     s.write(b);
     alreadySent=true;
     numSends++;
     println("Byte array sent");
   }
 }
 
}

// ServerEvent message is generated when a new client connects
// to an existing server.
void serverEvent(Server someServer, Client someClient) {
 background(0);
 fill(0,255,0);
 text("SERVER", 10, 15);
 fill(255);
 text("New client: " + someClient.ip(),10,25);
}

//

void keyPressed() {
 if(key=='s'){
   doSend=true;
 }
}

void keyReleased() {
 if(key=='s'){
   doSend=false;
   alreadySent=false;
 }
}

////////////////////////////////////////


CLIENT code here:


import processing.net.*;

//

Client c;

//

PFont font;

//

void setup(){

 size(200,200);

 c=new Client(this,"127.0.0.1",5000);

 //Text
 background(0);
 font = loadFont("f.vlw");
 textFont(font, 8);

}

void draw(){

 //Environment
 fill(0,0,0,5);
 rect(0,0,width,height);

 //Text
 fill(255,128,0);
 text("CLIENT", 10, 15);
 fill(255);
 text("Bytes available from server: "+c.available(),10,25);

}

// ClientEvent message is generated when the server
// sends a byte to an existing client.
void clientEvent(Client _c) {

 byte [] dataIn=c.readBytes();

 if(dataIn!=null){
   println(dataIn.length);
   //Use byte array to show img
   for(int x=0;x<width;x++){
     for(int y=0;y<height;y++){
       int loc=x+y*width;
       // bytes are from -128 to 127, this converts to 0 to 255
       int _r = dataIn[loc] & 0xff;
       int _g = dataIn[40000+loc] & 0xff;
       int _b = dataIn[2*40000+loc] & 0xff;
       //Set the color of each pixel in output
       color c= color(_r,_g,_b);
       set(x,y,c);
     }
   }
 }

}

void keyReleased() {
 if(key=='q'){
   c.stop();
 }
 else if(key=='c'){
   c.clear();
 }
}

////////////////////////////////////////

first run server sketch and then client

an image named "img.jpg" must be in server "data" folder to work

create a font named "f.vlw" and put it in server "data" folder

give focus to server window and press 's' to send the image to the client

THANKS!!! :)



Page Index Toggle Pages: 1