I am doing my programming assignment, but got failure during the connection.
Can anyone help me to figured it out?
Can anyone help me to figured it out?
Server program:
import processing.video.*; // Necessary for webcam
import processing.net.*; // Necessary for data communication
String yourName = "David:";
String msg = "";
String serverAddress = "127.0.0.1";
Capture img; // The local capture from a webcam
PImage imgLocal; // The local image (Left)
PImage imgReceived; // The received image (Right)
int enterClick =0;
PImage []imgF = new PImage [3];
int maxFlower =100;
int[] flowerX = new int [maxFlower];
int[] flowerY = new int [maxFlower];
int currFlower =0;
int mouseClick =0;
int R = 0;
Server s; // The Server
Client c; // The client
int serverPort = 12345; // The port of server
int stageNum = 0;
void setup()
{
frameRate(50); // slow down the frame rate
size( 640, 300 ); // Set the window size as 640 x 300
// By default, the client is not yet connected
// Create a blank image as a place holder
imgReceived = createImage(320, 300, RGB);
// Initialize the webcam
img = new Capture(this, 320, 240);
// Power on the webcam
img.start();
// Create a server object
s = new Server(this, serverPort);
for (int i=0; i<3; i++)
{
imgF[i] = loadImage("flower" + i + ".png");
}
}
void draw()
{
if ( img.available() == true )
{
background(255, 255, 255); // fill the background with white color
img.read(); // read a webcam image
image(img, 0, 0); // display the local webcam image
fill(0, 0, 0);
textSize(24);
text(yourName + msg, 0, 280);
if ( enterClick%4 == 1 )
{
processImage1();
}
else if ( enterClick %4 ==2)
{
filter(POSTERIZE,6);
}
else if ( enterClick %4==3)
{
processImage3();
}
for (int i=0; i<currFlower; i++)
{
imageMode(CENTER);
image(imgF[i%3], flowerX[i], flowerY[i], 50, 50);
flowerY[i] = flowerY[i]+1;
imageMode(CORNER);
}
imgLocal = get(0, 0, 320, 300); // Get the region from (0,0) to (320,300)
sendLocalImageToClient(); // Send the local image to client
receiveImageFromClient(); // Receive an image from client
image(imgReceived, 320, 0); // Show the image received from client at (320,0)
}
}
/**** DON'T MAKE ANY CHANGES BELOW THIS LINE ****/
void sendLocalImageToClient()
{
// Initialize dataToSend as NULL array
byte[] dataToSend = null ;
// Compress an image to a byte array
dataToSend = compress(imgLocal);
// Write the data packet to the client
if ( dataToSend != null ) {
s.write(dataToSend);
}
}
void receiveImageFromClient()
{
// Initialize dataReceived as NULL array
byte[] dataReceived = null ;
// Get a connected client
c = s.available();
if (c != null)
{
dataReceived = c.readBytes();
}
// Decompress the data to an image
if ( dataReceived != null ) {
decompress(dataReceived, imgReceived);
}
}
import javax.imageio.*;
import java.awt.image.*;
byte[] compress(PImage img) {
byte[] packet = null ;
try {
// We need a buffered image to do the JPG encoding
BufferedImage bimg = new BufferedImage( img.width, img.height, BufferedImage.TYPE_INT_RGB );
// Transfer pixels from localFrame to the BufferedImage
img.loadPixels();
bimg.setRGB( 0, 0, img.width, img.height, img.pixels, 0, img.width);
// Need these output streams to get image as bytes for UDP communication
ByteArrayOutputStream baStream = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baStream);
// Compress an image using "JPG" compression
// img (uncompressed) => bimg (compressed)
ImageIO.write(bimg, "jpg", bos);
// Get the byte array, which we will send out via UDP!
packet = baStream.toByteArray();
}
catch (Exception e) {
e.printStackTrace();
}
return packet;
}
void decompress(byte[] data, PImage receivedImg) {
try {
// Read incoming data into a ByteArrayInputStream
ByteArrayInputStream bais = new ByteArrayInputStream( data );
// We need to unpack JPG and put it in the PImage video
receivedImg.loadPixels();
// Make a BufferedImage out of the incoming bytes
BufferedImage bimg = ImageIO.read(bais);
// Put the pixels into the video PImage
bimg.getRGB(0, 0, receivedImg.width, receivedImg.height, receivedImg.pixels, 0, receivedImg.width);
receivedImg.updatePixels();
}
catch (Exception e) {
e.printStackTrace();
}
}
void keyPressed()
{
if (keyCode == BACKSPACE )
{
if ( msg.length() > 0 )
{
msg = msg.substring(0, msg.length()- 1);
}
}
else if ( keyCode == DELETE )
{
msg = "";
}
else if ( keyCode == ENTER)
{
enterClick= enterClick +1;
}
else if (keyCode == UP)
{
save("output.png");
}
else if ( key != CODED)
{
msg += key;
}
}
void mousePressed()
{
if (mouseButton==LEFT && currFlower< maxFlower)
{
flowerX[currFlower]=mouseX;
flowerY[currFlower]=mouseY;
currFlower = currFlower+1;
mouseClick = mouseClick+1;
}
else if (mouseButton== RIGHT)
{
for (int i=0; i<currFlower; i++)
{
flowerX[i] =1000;
flowerY[i] =1000;
currFlower = 0;
}
}
}
void processImage1()
{
img.loadPixels();
// Use for loops to access the array of pixels in an image
for (int y = 0; y < img.height; y++ )
{
for (int x = 0; x < img.width; x++ )
{
int loc = x + y*img.width;
// load the color value
float r = red (img.pixels [loc]);
float g = green(img.pixels[loc]);
float b = blue (img.pixels[loc]);
/******** image processing goes here **********/
{
float randomNum=random(0, 100);
if (randomNum<20)
{
r=0;
g=0;
b=0;
}
else if (randomNum>=20 && randomNum<=40)
{
r=255;
g=255;
b=255;
}
else
{
r=r;
g=g;
b=b;
}
}
// Set the display pixel to the image pixel
img.pixels[loc] = color(r, g, b); // new color
}
}
img.updatePixels(); // update pixels with new value
image(img, 0, 0); // display the image
}
void processImage3()
{
img.loadPixels();
// Use for loops to access the array of pixels in an image
for (int y = 0; y < img.height; y++ )
{
for (int x = 0; x < img.width; x++ )
{
int loc = x + y*img.width;
// load the color value
float r = red (img.pixels [loc]);
float g = green(img.pixels[loc]);
float b = blue (img.pixels[loc]);
/******** image processing goes here **********/
float gy=(r+b+g)/3;
if(gy<127)
{
r=0;
b=0;
g=0;
}else
{
r=255;
b=255;
g=255;
}
// Set the display pixel to the image pixel
img.pixels[loc] = color(r, g, b); // new color
}
}
img.updatePixels(); // update pixels with new value
image(img, 0, 0); // display the image
}