ScreenCapture with Robot Class (code not working)

i have an irl card that has the rgb value:11, 96, 133.
i made a code that, scans the screen for that color so when you have the app camera open it says kaart gevonden (found card in dutch) on the screen. but it is not working, any ideas why?
here is the code


int x=1;
int y=1;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
color pas = color(11, 96, 133);
 
Robot robot;
void setup() {
  size(1500, 3000);
  try {
    robot = new Robot();
  } 
  catch (Exception e) {
    println(e.getMessage());
  }
}
void draw() {
  background(0);
  Rectangle r = new Rectangle(y, x, width, height);
  BufferedImage img1 = robot.createScreenCapture(r);
  PImage img2 = new PImage(img1);
  image(img2, 0, 0);
  get(x,y);
  x++;
  y++;
  color colorFromGet = get(x,y);
     
     if(colorFromGet==(colorFromGet+pas)/2){
       textSize(50);
       text("kaart gevonden", width/2, height/2);
     
}
}

Hello,

This adapted code will:

  • screen capture a small area at the center of your display monitor
  • display the image at the center of the sketch window
  • check for color(0xFF, 0xFF, 0xFF) (white) at the center of the sketch window
  • I used white so I can move a window in and out of capture area
  • it will display "“kaart gevonden” if white is at center
// Screen Scanner
// v1.0.0
// GLV 2021-06-07

// Adapted from: 
// http://www.magicandlove.com/blog/tag/screen-capture/
// https://discourse.processing.org/t/screencapture-with-robot-class-code-not-working/30551

int x=1;
int y=1;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
color pas = color(0xFF, 0xFF, 0xFF);
 
Robot robot;
void setup() 
  {
  size(500, 600);
  try 
    {
    robot = new Robot();
    } 
  catch (Exception e) 
    {
    println(e.getMessage());
    }
  }

void draw() 
  {
  background(0);
  Rectangle r = new Rectangle(displayWidth/2-100, displayHeight/2-100, 300, 400);
  BufferedImage img1 = robot.createScreenCapture(r);
  PImage img2 = new PImage(img1);
  imageMode(CENTER);
  image(img2, width/2, height/2);
  
  //get(x,y); // Not used in this code!
  //x++;       
  //y++;
  
  color colorFromGet = get(width/2, height/2); //Gets color from center of sketch window   
  // Examples of what you can do here:
  //color colorFromGet = get(x, y);
  //color colorFromGet = get(mouseX, mouseY);
     
   if(colorFromGet == pas)
     {
     textSize(50);
     fill(0, 255, 0);
     textAlign(CENTER, CENTER);
     text("kaart gevonden", width/2, height/2);
     println("kaart gevonden"); 
     }
  println(frameRate);
  println(hex(get(mouseX, mouseY))); //Gets color from mosue location and displays as hex
  }

I just did a quick exploration of this; there are certainly other ways to do this.
You will have to go through code line by line and understand it.
Modify it as you wish.

There are resources (tutorials, references, examples, etc.) here:
https://processing.org/

Center of screen:

Center of screen with a window (this could be your app) moved into focus:

:)

1 Like

thank you, what did you change the pas color into? it was the wrong rgb value and when i wanted to change it i saw it was something i didnt recognise

I already answered that.

It was the correct RGB value for the code I provided to test this.

There are tutorials to help you understand color.

:)

what kind of colour did you change it into? is it binary rgb?

https://processing.org/reference/color_datatype.html

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

// These all represent white

color c;

void setup()
{
c = #FFFFFF;
println(red(c), green(c), blue(c));

c = color (255, 255, 255);
println(red(c), green(c), blue(c));

//0xFF = 255 = 0b11111111
c = color(0xFF, 0xFF, 0xFF); //Hexadecimal notation 
println(red(c), green(c), blue(c));
int b = 0b11111111; //Binary notation
c = color(b, b, b);
println(red(c), green(c), blue(c));

c = 0xFFFFFFFF;
println(red(c), green(c), blue(c));
}

``:)``

thank you for your help. sadly it doesnt work because on different distances it has a different colour value and wont recognise it. but thanks anyway

I see that as an opportunity and a challenge.

Consider using these:

I provided you with a tool to display the colors on the screen to get a sense of them:

println(hex(get(mouseX, mouseY))); //Gets color from mouse location and displays as hex

You can use this to get more of the color details and then write some code for that.
println(hex(c), red(c), green(c), blue(c), hue(c), saturation(c), brightness(c)); //Gets color from mouse location and displays as hex

I found hue() to be useful and got this working for a range of hues (desktop folder in a screen capture in my case).

You are going to have to read the Processing color tutorial and look through color references and examples.

:)

I explained it in your other thread

I cannot understand why you ignored it

There are so many errors…

i tried to use it but it only displayed one colour even if i moved my mouse arround

Try this program from examples:

image

Add this line of code to draw():
println(mouseX, mouseY, hex(get(mouseX, mouseY)));

:)