BLUSHIF

Server

Nov 20th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 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. import java.net.UnknownHostException;
  7. import java.net.InetAddress;
  8. Server s;
  9. Client c;
  10. int imageByteCount = 640/2 * 480/2 * 3;
  11. byte[] imageBuffer = new byte[imageByteCount];
  12. String input;
  13. PImage vi;
  14. Capture video;
  15. OutputStream outputStream = null;
  16. void setup() {
  17. size(640, 480);
  18. try {
  19. InetAddress addr = InetAddress.getLocalHost();
  20. String raw_addr = addr.toString();
  21. String[] list = split(raw_addr, '/');
  22. println("your ip host address is: "+list[1]);
  23. }
  24. catch (UnknownHostException e) {
  25. println(e);
  26. }
  27. video = new Capture(this, 640/2, 480/2);
  28. frameRate(5);
  29. s = new Server(this, 12345);
  30. video.start();
  31. }
  32. void draw() {
  33. s.write(broadcast(video));
  34. //video.resize(80, 60);
  35. //image(video, 40, 30);
  36. while (c.available() >= imageByteCount) {
  37. imageBuffer=c.readBytes();
  38. PImage fback=Recieve(imageBuffer, 640/2, 480/2, 3);
  39. image(fback, 0, 0);
  40. }
  41. }
  42. byte[] broadcast(PImage img) {
  43. BufferedImage bimg = new BufferedImage( img.width, img.height,
  44. BufferedImage.TYPE_INT_RGB );
  45. img.loadPixels();
  46. bimg.setRGB( 0, 0, img.width, img.height, img.pixels, 0, img.width);
  47. ByteArrayOutputStream baStream = new ByteArrayOutputStream();
  48. BufferedOutputStream bos = new BufferedOutputStream(baStream);
  49. try {
  50. ImageIO.write(bimg, "jpg", bos);
  51. }
  52. catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. byte[] packet = baStream.toByteArray();
  56. return packet;
  57. }
  58. PImage Recieve(byte[] data, int w, int h, int ch) {
  59. PImage outImage = new PImage(w, h, RGB);
  60. outImage.loadPixels();
  61. for (int i = 0; i < w*h; i++) {
  62. outImage.pixels[i] = color(data[i*ch], data[i*ch+1], data[i*ch+2]);
  63. }
  64. outImage.updatePixels();
  65. return outImage;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment