Client Program:
import processing.video.*; // Necessary for webcam
import processing.net.*; // Necessary for data communication
int stageNum = 0;
Capture img;
PImage imgLocal;
PImage imgReceived;
Client c; // The Client
String serverAddress = "xxx.xxx.xxx.xxx"; // The IP address of the server
int serverPort = 12345;
String yourName = "Client: ";
String msg = " ";// The server port
int mirror =0;
int processImage = 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();
}
void draw()
{
if (stageNum == 0)
{
background(41, 22, 95);
fill(255, 255, 255 );
textSize(24);
text("Type in Server IP address and press ENTER:", 100, 100);
text(serverAddress, 100, 150);
}
else if ( stageNum == 1)
{
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 (mirror%3 ==1)
{
mirror1();
}
else if ( mirror % 3 ==2)
{
mirror2();
}
if (processImage %3 == 1)
{
processImage1();
}
else if (processImage %3 ==2)
{
processImage2();
}
textAlign(LEFT);
textSize(18);
fill(255,0,0);
text("12204501 Section2", 10, 15);
imgLocal = get(0, 0, 320, 300); // Get the region from (0,0) to (320,300)
sendLocalImageToServer(); // Send the local image to server
receiveImageFromServer(); // 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 sendLocalImageToServer()
{
// Initialize dataToSend as NULL array
byte[] dataToSend = null ;
// Compress an image to a byte array
dataToSend = compress(imgLocal);
// Write the data packet to the server
if ( dataToSend != null )
{
c.write(dataToSend);
}
}
void receiveImageFromServer()
{
// Initialize dataReceived as NULL array
byte[] dataReceived = null ;
// Receive data from server
if (c.available() > 0)
{
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 (stageNum==0) {
if (keyCode == BACKSPACE ) {
if ( serverAddress.length() > 0 )
{
serverAddress = serverAddress.substring(0, serverAddress.length()-1);
}
}
else if ( keyCode == DELETE )
{
serverAddress = "";
}
else if ( keyCode == ENTER )
{
if ( stageNum == 0 )
{
stageNum = 1;
// Create a client object to connect with the server
c = new Client(this, serverAddress, serverPort);
}
}
else if (key != CODED) {
serverAddress += key;
}
}
else
{
if ( keyCode == BACKSPACE )
{
if ( msg.length() > 0)
{
msg = msg.substring (0, msg.length() -1);
}
}
else if (keyCode == DELETE )
{
msg = " ";
}
else if (keyCode == ENTER)
{
processImage=processImage+1;
}
else if (keyCode == UP)
{
save("output.png");
}
else if (key != CODED)
{
msg += key;
}
}
}
void mousePressed()
{
if (mousePressed==true)
{
mirror = mirror+1;
}
}
void mirror1()
{
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/2; x++ )
{
int loc = x + y*img.width;
int loc2 =(img.width-x-1)+(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]);
float r2 = red (img.pixels [loc2]);
float g2 = green(img.pixels[loc2]);
float b2 = blue (img.pixels[loc2]);
/******** image processing goes here **********/
// Set the display pixel to the image pixel
img.pixels[loc] = color(r, g, b); // new color
img.pixels[loc2]= color(r, g, b);
}
}
img.updatePixels(); // update pixels with new value
image(img, 0, 0); // display the image
}
void mirror2()
{
img.loadPixels();
// Use for loops to access the array of pixels in an image
for (int y = 0; y < img.height/2; y++ )
{
for (int x = 0; x < img.width; x++ )
{
int loc = x + y*img.width;
int loc2 =(x)+(img.height-y-1)*img.width;
// load the color value
float r = red (img.pixels [loc]);
float g = green(img.pixels[loc]);
float b = blue (img.pixels[loc]);
float r2 = red (img.pixels [loc2]);
float g2 = green(img.pixels[loc2]);
float b2 = blue (img.pixels[loc2]);
/******** image processing goes here **********/
// Set the display pixel to the image pixel
img.pixels[loc] = color(r, g, b); // new color
img.pixels[loc2]= color(r, g, b);
}
}
img.updatePixels(); // update pixels with new value
image(img, 0, 0); // display the image
}
void processImage2()
{
img.loadPixels();
// Use for loops to access the array of pixels in an image
for (int y = 0; y < img.height/2; y++ )
{
for (int x = 0; x < img.width; x++ )
{
int loc = x + y*img.width;
int loc2 =(x)+(img.height-y-1)*img.width;
// load the color value
float r = red (img.pixels [loc]);
float g = green(img.pixels[loc]);
float b = blue (img.pixels[loc]);
float r2 = red (img.pixels [loc2]);
float g2 = green(img.pixels[loc2]);
float b2 = blue (img.pixels[loc2]);
/******** image processing goes here **********/
float gy = (r+b+g)/3;
r=gy;
g=gy;
b=gy;
float gy2= (r2+b2+g2)/3;
r2=gy2;
g2=gy2;
b2=gy2;
// Set the display pixel to the image pixel
img.pixels[loc] = color(r2, g2, b2); // new color
img.pixels[loc2]= color(r, g, b);
}
}
img.updatePixels(); // update pixels with new value
image(img, 0, 0); // display the image
}
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/2; x++ )
{
int loc = x + y*img.width;
int loc2 =(img.width-x-1)+(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]);
float r2 = red (img.pixels [loc2]);
float g2 = green(img.pixels[loc2]);
float b2 = blue (img.pixels[loc2]);
/******** image processing goes here **********/
// Set the display pixel to the image pixel
img.pixels[loc] = color(r2, g2, b2); // new color
img.pixels[loc2]= color(r, g, b);
}
}
img.updatePixels(); // update pixels with new value
image(img, 0, 0); // display the image
}