Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import processing.net.*;
- import java.io.*;
- import processing.video.*;
- import java.awt.image.BufferedImage;
- import javax.imageio.*;
- import java.net.UnknownHostException;
- import java.net.InetAddress;
- Server s;
- Client c;
- String input;
- PImage vi;
- Capture video;
- OutputStream outputStream = null;
- void setup() {
- size(640, 480);
- try {
- InetAddress addr = InetAddress.getLocalHost();
- String raw_addr = addr.toString();
- String[] list = split(raw_addr, '/');
- println("your ip host address is: "+list[1]);
- }
- catch (UnknownHostException e) {
- println(e);
- }
- video = new Capture(this, 640/2, 480/2);
- frameRate(5);
- s = new Server(this, 12345);
- video.start();
- }
- void draw() {
- s.write(broadcast(video));
- video.resize(80, 60);
- image(video, 40, 30);
- if (s.available() != null) {
- PImage fback=Recieve(loadBytes(c.readString()), 640, 480, 3);
- image(fback, 0, 0);
- }
- }
- byte[] broadcast(PImage img) {
- BufferedImage bimg = new BufferedImage( img.width, img.height,
- BufferedImage.TYPE_INT_RGB );
- img.loadPixels();
- bimg.setRGB( 0, 0, img.width, img.height, img.pixels, 0, img.width);
- ByteArrayOutputStream baStream = new ByteArrayOutputStream();
- BufferedOutputStream bos = new BufferedOutputStream(baStream);
- try {
- ImageIO.write(bimg, "jpg", bos);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- byte[] packet = baStream.toByteArray();
- return packet;
- }
- PImage Recieve(byte[] data, int w, int h, int ch) {
- PImage outImage = new PImage(w, h, RGB);
- outImage.loadPixels();
- for (int i = 0; i < w*h; i++) {
- outImage.pixels[i] = color(data[i*ch], data[i*ch+1], data[i*ch+2]);
- }
- outImage.updatePixels();
- return outImage;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement