/
ProcessingOverWebSocket.pde
109 lines (83 loc) · 2.57 KB
/
ProcessingOverWebSocket.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Broadcast the visual result of this sketch as a jpg image
stream to a websocket. This is a rough proof of concept
and is not really ready for easy use - but you can copy
and paste parts of it. if it does not work or chrashes
your machine, i am not responsible for that! ;)
Oh! And you must have a browser, that supports WebSockets,
e.g. Chrome, Chromium or Safari.
It is based on the UDP streaming example from Daniel
Shifman you can find here:
http://www.shiffman.net/2010/11/13/streaming-video-with-udp-in-processing/
For the WebSocketP5 functionality you need this library:
http://p5.twelvebytes.net/websocketP5-0.1.2/
***
HOW TO SEE SOMETHING:
1) make sure you have the above mentioned library
2) run this sketch
3) point your browser to http://localhost:8080
4) be amazed and pay resepct to the mystic digital things
5) write interesting things with this!
TATA!
have fun! :)
*/
// For the WebSocket
import muthesius.net.*;
import org.webbitserver.*;
// For image compression
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;
// For Base64
import org.apache.commons.codec.binary.*;
// Let's roll!
WebSocketP5 ws;
PFont font;
void setup() {
size(320, 240); // do not use more than this, it'll slow down!
smooth();
font = createFont("Monaco", 27);
textFont(font);
ws = new WebSocketP5(this, 8080);
}
float anim = 0;
float speed= 0.005;
void draw() {
anim = (anim+speed)-(int)anim;
background(127);
noStroke();
fill(255);
text("FPS: "+frameRate, 10, 35);
rect(anim*width-10, mouseY, 10, height);
// after drawing, broadcast the image:
broadcastOutput();
}
void broadcastOutput() {
// We need a buffered image to do the JPG encoding
int w = width;
int h = height;
BufferedImage b = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// Transfer pixels from localFrame to the BufferedImage
loadPixels();
b.setRGB( 0, 0, w, h, pixels, 0, w);
// Need these output streams to get image as bytes for UDP
ByteArrayOutputStream baStream = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baStream);
// JPG compression into BufferedOutputStream
// Requires try/catch
try {
ImageIO.write(b, "jpg", bos);
}
catch (IOException e) {
println("could not encode image");
return;
}
// Get the byte array, which we will send out via UDP!
try {
String out = new String(Base64.encodeBase64(baStream.toByteArray(), false));
ws.broadcast("data:image/*;base64,"+out);
}
catch(Exception e) {
e.printStackTrace();
}
}