This repository was archived by the owner on Dec 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaspiLedClient.java
More file actions
executable file
·95 lines (79 loc) · 3.16 KB
/
RaspiLedClient.java
File metadata and controls
executable file
·95 lines (79 loc) · 3.16 KB
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
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
public class RaspiLedClient {
public static void main(String[] args) throws Exception{
//screen size
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenW = (int)screenSize.getWidth();
int screenH = (int)screenSize.getHeight();
int pixelSkip = 1;
String hex;
//variables for taking the screenshot
int[] screenData;
BufferedImage screenshot;
Robot bot;
Rectangle dispBounds = new Rectangle(new Dimension(screenW,screenH));
String restPort = "42069";
int udpPort = 1337;
if (args.length == 3){
restPort = args[1];
udpPort = Integer.parseInt(args[2]);
}else if (args.length != 1){
System.out.println("Usage:\njava RaspiLedClient <host>\njava RaspiLedClient <host> <rest_port> <udp_port>");
System.exit(1);
}
InetAddress ipAddress = InetAddress.getByName(args[0]);
String urlString = new StringBuilder("http:/").append(ipAddress).append(":").append(restPort).append("/set/stream").toString();
URL streamUrl = new URL(urlString);
try{
HttpURLConnection con = (HttpURLConnection) streamUrl.openConnection();
con.setRequestMethod("GET");
if (con.getResponseCode() != 200){
System.out.println("ERROR: Stream mode could not be started on backend.");
System.exit(1);
}
}catch(ConnectException e){
System.out.println("ERROR: Backend unavailable.");
System.exit(1);
}
DatagramSocket clientSocket = new DatagramSocket();
try {
bot = new Robot();
while(true){
screenshot = bot.createScreenCapture(dispBounds);
screenData = ((DataBufferInt)screenshot.getRaster().getDataBuffer()).getData();
hex = getAvgScreenColor(screenW, screenH, pixelSkip, screenData);
System.out.println("RGB = " + hex);
clientSocket.send(new DatagramPacket(hex.getBytes(), 6, ipAddress, udpPort));
}
}
catch (AWTException e) {
System.out.println("Robot class not supported by your system!");
System.exit(0);
}
clientSocket.close();
}
private static String getAvgScreenColor(int screenW, int screenH, int pixelSkip, int[] screenData){
int pixel;
int r = 0;
int g = 0;
int b = 0;
int[] col = new int[3];
String hex;
for(int i = 0; i < screenH; i += pixelSkip){
for(int j = 0; j < screenW; j += pixelSkip){
pixel = screenData[ i*screenW + j ];
r += 0xff & (pixel>>16);
g += 0xff & (pixel>>8 );
b += 0xff & pixel;
}
}
col[0] = r / (screenH/pixelSkip * screenW/pixelSkip);
col[1] = g / (screenH/pixelSkip * screenW/pixelSkip);
col[2] = b / (screenH/pixelSkip * screenW/pixelSkip);
hex = String.format("%02x%02x%02x", col[0], col[1], col[2]);
return hex;
}
}