segunda-feira, 29 de junho de 2009

Intervenção - Código do processing

Código do servidor

import fullscreen.*;
import processing.net.*;
import com.sun.image.codec.jpeg.*;
import hypermedia.video.*;


FullScreen fs;

OpenCV cam;
Server s;
Client c;
boolean startServing=false;
int port=12345;
int input;

int qNum = 256;
int qLargura, qAltura;
int numPixels;

float verde = 1;
float azul = 1;
float verm = 1;
PFont fontTexto;


int inc = 0;
int veloc = 12;

void setup() {

size(1536,768);
cam = new OpenCV(this);
cam.capture( width/4, height/4 , 5);
s = new Server(this,port);
frameRate(veloc);
fs = new FullScreen(this);
fs.enter();

fontTexto = createFont(PFont.list()[2], 18);
textFont(fontTexto, 12);
background(0);
}

void serverEvent(Server s, Client c) {
startServing=true;
fill(0,255,0);
background(0);
text("Cliente conectado: " + c.ip(), 10,height-10);
}

void draw(){

inc++;
int inc2 = inc%(veloc/2);
int cwidth = width/4;
int cheight = height/4;
cam.read();
cam.brightness(40);
cam.contrast(20);
PImage img = cam.image();
if(startServing && (inc2==0) ){
byte[] jpgBytes=jpgFromPImage(img);
s.write(jpgBytes.length/256); //high bit
s.write(jpgBytes.length%256);//low bit
s.write(jpgBytes);
} else {
fill(255,0,0);
text("Aguardando Cliente...", 10,height-10);
}

c = s.available();
if (c != null) {
input = c.read();
qNum = input;
}
if(input == 'g') {
qNum = 256;
} else if(input == 't') {
qNum = 192;
} else if(input == 'c') {
qNum = 96;
} else if(input == 'u') {
verde *= 1.2;
} else if(input == 'b') {
qNum = 256;
}else if(input == 's') {
qNum = 192;;
}else if(input == 'w') {
qNum = 96;
}

int qNum2 = qNum*2;
qLargura = width/qNum2;
qAltura = height/qNum;
numPixels = qLargura*qAltura;
int dimension = (img.width*img.height);
img.loadPixels();
for (int y=0; y < cheight; y += qAltura) {
for (int x=0; x < cwidth; x += qLargura) {
float r = 0;
float g = 0;
float b = 0;
for (int yIndex = 0; yIndex < qAltura; yIndex++) {
for (int xIndex = 0; xIndex < qLargura; xIndex++) {
r += red(img.pixels[x+y*cwidth+xIndex+yIndex*cwidth]);
g += green(img.pixels[x+y*cwidth+xIndex+yIndex*cwidth])*1.2;
b += blue(img.pixels[x+y*cwidth+xIndex+yIndex*cwidth]);
}
}
r /= numPixels;
g /= numPixels;
b /= numPixels;
stroke(255, 255, 255, 100);
fill(r, g, b);
rect(x*4, y*4, qLargura*4, qAltura*4);
}
}
}

byte[] jpgFromPImage(PImage srcimg){
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedImage img = new BufferedImage(srcimg.width, srcimg.height, 2);
img = (BufferedImage)createImage(srcimg.width, srcimg.height);
for(int i = 0; i < srcimg.width; i++) {
for(int j = 0; j < srcimg.height; j++) {
int id = j*srcimg.width+i;
img.setRGB(i,j, srcimg.pixels[id]);
}
}
try{
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam encpar = encoder.getDefaultJPEGEncodeParam(img);
encpar.setQuality(0.4,false); // 0.0-1.0, force baseline
encoder.setJPEGEncodeParam(encpar);
encoder.encode(img);
}
catch(FileNotFoundException e){
System.out.println(e);
}
catch(IOException ioe){
System.out.println(ioe);
}
return out.toByteArray();
}

Código do Cliente

import processing.net.*;
import com.sun.image.codec.jpeg.*;

Client c;
int qNum = 40;
String ip = "127.0.0.1";
int port = 12345;

void setup(){
size(640,480);
c = new Client(this, ip, port);
}

void draw(){
if(c.available() >0){
int imageSize=c.read()*256+c.read();
while(c.available() byte[] dataIn=new byte[imageSize];
c.readBytes(dataIn);
PImage i=loadPImageFromBytes(dataIn,this);
image(i,0,0);
}

if(keyPressed) {
if(key == 'A' || key == 'a') {
qNum = 60;
} else if(key == 'S' || key == 's') {
qNum = 40;
} else if(key == 'D' || key == 'd') {
qNum = 20;
} else if(key == 'F' || key == 'f') {
qNum = 10;
} else if(key == 'G' || key == 'g') {
qNum = 5;
}
}
c.write(qNum);
}

PImage loadPImageFromBytes(byte[] b,PApplet p){
Image img = Toolkit.getDefaultToolkit().createImage(b);
MediaTracker t=new MediaTracker(p);
t.addImage(img,0);
try{
t.waitForAll();
}
catch(Exception e){
println(e);
}
return new PImage(img);
}

Nenhum comentário:

Postar um comentário