java robot slow to getRed() in loop; how to speed up?

This code is supposed to watch a window in another application to track the movement of a changing musical pitch represented by a blue horizontal line that always appears between (280, 230) and (280, 600) when a note is present but its unbelievably slow.

It searches for first pixel with the red value of '71' in a 370 pixel tall, 1 pixel wide column on my desktop, and returns it's y position. Finding this colour at, say (232,280) seems relatively quick, but finding it at (589,280) takes ten whole seconds from pressing the 'play' button, and around 6 seconds if the position of the blue line changes from y = 232 to y = 589.

import java.awt.Robot;
import java.awt.AWTException;

Robot robot;

void setup() {

    try {
    robot = new Robot();
  }
  catch (AWTException cause) {
    exit();
    throw new RuntimeException(cause);
  }
}
//
void draw() {

  for (int i = 230; i < 601; i++) 
  {
    int p = robot.getPixelColor(280 , i).getRed();

    if (p == 71) {  //      71, 182, 240 is rgb to look for
      println(i);
      break;
      }
  }
}

My PC is plenty fast enough, it has a fairly decent graphics card even, but I'm shocked how slowly this runs. I had hoped it would look at all the pixels in maybe 1/50 of a second; I could probably sit there with a ruler and count the pixels off on that faster.

Is there any way to get this considerably faster?

Thanks

Answers

  • edited March 2018

    I was under the impression that anything between ' and ' after pressing the 'code' C button would be treated as code. Would be great if that could be done...

  • edited March 2018

    No the back-apostrophe is used for inline code.

    For a code block, make sure there is a blank line before and after the block, then select the code block and click on C. This will indent the code by 4 spaces.

    I have corrected your original post.

  • edited March 2018

    Thanks

  • Thanks Quark. IDo you mean the 'C' on the line above where I'm typing now? If so I did everything as per instructions, so I'm not sure what happened.

  • it has a fairly decent graphics card

    getting the values back from a video card is often slow - the bandwidth is very asymmetrical, fast to write, slow to read.

    it might be more efficient to get the pixels in bulk and iterate through them rather than calling getPixelColour 370 times

  • There seems to be a 'rectangle' method in robot class, I'll look into whether thta can be set to grab a screenshot 1 x 370 pixels, then maybe put that into a Pimage and do the code on that? Is that what you mean?

    On a slightly different note, the etiquette for 'Did this answer your question? Yes/No' is very unclear, as are th econsequences of either choice. I don't want to offend by marking 'no', but I dont want this thread to effectively close because its marked as 'answered.' It's very unfamiliar; plus this forum repeatedly crashes firefox.

  • yes, rectangle. whether it's any faster depends on the input, i guess. if it's 20 times slower but it's called 20 times less frequently then you win.

    mark the question answered when you have enough info. it's nice to know a question is finished but ignore the nagging until then.

    it's fine on my firefox, has been for 10 years now 8)

  • reloaded firefox, all seems well with it now.

Sign In or Register to comment.