Take a screen shot of the screen?

edited May 2015 in How To...

Hey guys, I was wondering if processing could take pictures of my screen. How could I start this code? I want it to take a picture every millisecond. Thanks in advance.

Answers

  • Answer ✓

    If you mean canvas shots, a simple get() will do: https://processing.org/reference/get_.html

  • Thank you but I meant take a screen shot of my desktop as well. Whatever is on my desktop. Will saveFrame() work?

  • Processing is only about the canvas it provides. Anything else, we gotta find a library to do that! :(|)

  • I see, thank you for your time!

  • edited November 2014

    Every millisecond sounds a bit much, that's 1000 times per second. The following example shows you how to get a screenshot of the whole screen into a PImage that can be used and displayed in Processing. It takes a screenshot every 60th frame, so about once every second.

    Note that taking screenshots of the screen that you display on, will result in a feedback loop! :-)

    Code Example

    import java.awt.*;
    PImage screenshot;
    
    void setup() {
      size(1280, 720);
    }
    
    void draw() {
      if (frameCount % 60 == 0) screenshot();
      if (screenshot != null) image(screenshot, 0, 0, width, height);
    }
    
    void screenshot() {
      try {
        screenshot = new PImage(new Robot().createScreenCapture(new Rectangle(0, 0, displayWidth, displayHeight)));
      } catch (AWTException e) { }
    }
    
  • Amnon thanks but what is java.awt , null, and how did we all of a sudden make a new Robot function? In line 15.

  • edited November 2014

    Like GoToLoop said, this isn't possible in Processing. But since Processing is Java, you have access to all the Java code / classes. All you have to do is add the relevant imports to the top of your sketch. In Java there happens to be a screen capture method in the Robot class, which was used in the example above.

    Often when something is not possible in Processing or one of it's libraries, it is possible in Java or one it's libraries. For more info on the available classes in Java, see: https://docs.oracle.com/javase/7/docs/api/overview-summary.html

    Most of the things in the screenshot() method are Java, not Processing. Null is (also) part of Processing however. It allows you to see if something valid is there. For example an object, a loaded image, font, etc. See: https://www.processing.org/reference/null.html Checking againt null is a safeguard for nullpointerexceptions.

  • edited July 2018

    @amnon's tweaked version w/ detailed library imports & cached pre-instantiations: :ar!

    /**
     * Robot Screenshots (v2.23)
     * by  Amnon (2014/Nov/11)
     * mod GoToLoop
     *
     * Forum.Processing.org/two/discussion/8025/
     * take-a-screen-shot-of-the-screen#Item_8
     */
    
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.AWTException;
    
    PImage screenshot;
    Rectangle dimension;
    Robot robot;
    
    void setup() {
      size(1280, 720);
    
      smooth(3);
      frameRate(.5);
    
      colorMode(RGB);
      imageMode(CORNER);
      background((color) random(#000000));
    
      screenshot = createImage(displayWidth, displayHeight, ARGB);
      dimension  = new Rectangle(displayWidth, displayHeight);
    
      try {
        robot = new Robot();
      }
      catch (AWTException cause) {
        println(cause);
        exit();
      }
    }
    
    void draw() {
      image(grabScreenshot(screenshot, dimension, robot), 0, 0, width, height);
    }
    
    static final PImage grabScreenshot(PImage img, Rectangle dim, Robot bot) {
      //return new PImage(bot.createScreenCapture(dim));
    
      bot.createScreenCapture(dim).getRGB(0, 0
        , dim.width, dim.height
        , img.pixels, 0, dim.width);
    
      img.updatePixels();
      return img;
    }
    
Sign In or Register to comment.