BLUSHIF

client.pde

Nov 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import processing.net.*;
  2. import java.io.*;
  3. import processing.video.*;
  4. import java.awt.image.BufferedImage;
  5. import javax.imageio.*;
  6. Client c;
  7. Capture video;
  8. PImage vi;
  9. OutputStream outputStream = null;
  10. void setup() {
  11. size(640, 480);
  12. video = new Capture(this, 640/2, 480/2);
  13. frameRate(5);
  14. c = new Client(this, "192.168.56.1", 12345);
  15. video.start();
  16. }
  17. void draw() {
  18. c.write(broadcast(video));
  19. //video.resize(80, 60);
  20. //image(video, 40, 30);
  21. if (c.available() > 0) {
  22. PImage fback=Recieve(c.readBytes(), 640/2, 480/2, 3);//changed
  23. image(fback, 0, 0,width,height);
  24. }
  25. }
  26. byte[] broadcast(PImage img) {
  27. BufferedImage bimg = new BufferedImage( img.width, img.height,
  28. BufferedImage.TYPE_INT_RGB );
  29. img.loadPixels();
  30. bimg.setRGB( 0, 0, img.width, img.height, img.pixels, 0, img.width);
  31. ByteArrayOutputStream baStream = new ByteArrayOutputStream();
  32. BufferedOutputStream bos = new BufferedOutputStream(baStream);
  33. try {
  34. ImageIO.write(bimg, "jpg", bos);
  35. }
  36. catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. byte[] packet = baStream.toByteArray();
  40. return packet;
  41. }
  42. PImage Recieve(byte[] data, int w, int h, int ch) {
  43. PImage outImage = new PImage(w, h, RGB);
  44. outImage.loadPixels();
  45. for (int i = 0; i < w*h; i++) {
  46. outImage.pixels[i] = color(data[i*ch], data[i*ch+1], data[i*ch+2]);
  47. }
  48. outImage.updatePixels();
  49. return outImage;
  50. }
Add Comment
Please, Sign In to add comment