-a- even it is not your code and you not understand it you might have a idea what it is for?
-
- what is connected to serial port
-
- why you take screenshots of a PC screen and what you calculate and send?
-b- ports must exist and be free
use
printArray(Serial.list()); // show all ports
-c- posting code here please use the
</> Preformatted text
from the post edit window menu
-d- at beginning use in setup()
frameRate(1);
to control FPS == lines to port ??
later can optimize or disable.
-e- a little cleanup of your code
import java.awt.Robot; //java library that lets us take screenshots
// https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html
import java.awt.AWTException;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Dimension;
import processing.serial.*; //library for serial communication
Serial port; //creates object “port” of serial class
Robot robby; //creates object “robby” of robot class
void setup() {
size(100, 100); //window size (doesn’t matter)
printArray(Serial.list()); // show all ports
port = new Serial(this, Serial.list()[0], 9600); //set baud rate
try { //standard Robot class error check
robby = new Robot();
}
catch (AWTException e) {
println("Robot class not supported by your system!");
exit();
}
frameRate(1); // start testing with one line per second send to ??
// disable for max ? 60 FPS ( and lines )
}
int pixel; //ARGB variable with 32 int bytes where //sets of 8 bytes are: Alpha, Red, Green, Blue
float r=0, g=0, b=0;
BufferedImage screenshot;
int i=0, j=0;
int w = 1368, h = 928;
int d = 2, n=w*h/(d*d); // w * h = 1269504 n 317376
// https://discourse.processing.org/t/this-code-works-in-my-friends-pc-but-i-get-error/13859
// We both changed the resolution from 1368x928 to 1920x1080.
void draw() {
background(r, g, b); //make window background average color
BufferedImage screenshot = robby.createScreenCapture(new Rectangle(new Dimension(w, h))); //1368*928
for (i =0; i<w; i=i+d) { //I skip every alternate pixel making my program 4 times faster
for (j=0; j<h; j=j+d) {
pixel = screenshot.getRGB(i, j);//the ARGB integer has the colors of pixel (i,j)
r = r+(int)(255&(pixel>>16)); //add up reds
g = g+(int)(255&(pixel>>8)); //add up greens
b = b+(int)(255&(pixel)); //add up blues
}
}
r=r/(n); //average red (remember that I skipped ever alternate pixel) 317376
g=g/(n); //average green
b=b/(n); //average blue
port.write(0xff); //write marker (0xff) for synchronization
port.write((byte)(r)); //write red value
port.write((byte)(g)); //write green value
port.write((byte)(b)); //write blue value
delay(10); //delay for safety
}
p.s. your code runs here ( win 10 / processing 3.5.3 / nothing serial connected / original screen size setting )
there are tools so you can readback your screen size and use in the program automatically:
import java.awt.Robot; //java library that lets us take screenshots
// https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html
import java.awt.AWTException;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Dimension;
// kll added for screen shot file and auto screen size adjust
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import processing.serial.*; //library for serial communication
Serial port; //creates object “port” of serial class
Robot robby; //creates object “robby” of robot class
Dimension myscreen;
Rectangle screenRect;
// https://discourse.processing.org/t/this-code-works-in-my-friends-pc-but-i-get-error/13859
// We both changed the resolution from 1368x928 to 1920x1080.
int w = 1368, h = 928;
int d = 2, n=w*h/(d*d); // w * h = 1269504 n 317376
int pixelc; //ARGB variable with 32 int bytes where //sets of 8 bytes are: Alpha, Red, Green, Blue
float r=0, g=0, b=0;
BufferedImage screenshot;
int i=0, j=0;
void setup() {
size(300, 100); //window size (doesn’t matter)
printArray(Serial.list()); // show all ports
port = new Serial(this, Serial.list()[0], 9600); //set baud rate
try { //standard Robot class error check
robby = new Robot();
}
catch (AWTException e) {
println("Robot class not supported by your system!");
exit();
}
//frameRate(1); // start testing with one line per second send to ?? // disable for max ? 60 FPS ( and lines )
myscreen = Toolkit.getDefaultToolkit().getScreenSize();
screenRect = new Rectangle(myscreen);
w = myscreen.width;
h = myscreen.height;
n = w*h/(d*d);
println(w +"*"+ h+" w*h "+w*h+" n "+n);
}
void draw() {
background(r, g, b); //make window background average color
surface.setTitle(nf(frameRate,0,1)+"FPS");
screenshot = robby.createScreenCapture(new Rectangle(new Dimension(w, h))); //1368*928
for (i =0; i<w; i=i+d) { //I skip every alternate pixel making my program 4 times faster
for (j=0; j<h; j=j+d) {
pixelc = screenshot.getRGB(i, j);//the ARGB integer has the colors of pixel (i,j)
r = r+(int)(255&(pixelc>>16)); //add up reds
g = g+(int)(255&(pixelc>>8)); //add up greens
b = b+(int)(255&(pixelc)); //add up blues
}
}
r=r/(n); //average red (remember that I skipped ever alternate pixel) 317376
g=g/(n); //average green
b=b/(n); //average blue
port.write(0xff); //write marker (0xff) for synchronization
port.write((byte)(r)); //write red value
port.write((byte)(g)); //write green value
port.write((byte)(b)); //write blue value
delay(10); //delay for safety
}
void keyPressed() {
if ( key == 's' ) {
try {
ImageIO.write(screenshot, "png", new File(sketchPath("")+'/'+"data/robotshot.png"));
println("saved");
}
catch ( IOException ex) {
println(ex);
}
}
}