We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProcessing DevelopmentLibraries,  Tool Development › Capturing the Screen / Taking Screenshots
Page Index Toggle Pages: 1
Capturing the Screen / Taking Screenshots (Read 2624 times)
Capturing the Screen / Taking Screenshots
Dec 29th, 2007, 7:43pm
 
Hi all,

I've been messing around with ways of getting a screenshot into Processing to use as the basis of some image manipulations.  I came up with some reasonably functional code and thought I'd post it here in case anyone else needed to do the same thing.


import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

PImage screenShot;

static public void main(String args[]) {
 PApplet.main(new String[] {
   "--present", "shooter"  }
 );
}

void setup() {
 size(screen.width, screen.height);
 screenShot = getScreen();
}

void draw () {
 image(screenShot,0,0, width, height);
}

PImage getScreen() {
 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 GraphicsDevice[] gs = ge.getScreenDevices();
 DisplayMode mode = gs[0].getDisplayMode();
 Rectangle bounds = new Rectangle(0, 0, mode.getWidth(), mode.getHeight());
 BufferedImage desktop = new BufferedImage(mode.getWidth(), mode.getHeight(), BufferedImage.TYPE_INT_RGB);

 try {
   desktop = new Robot(gs[0]).createScreenCapture(bounds);
 }
 catch(AWTException e) {
   System.err.println("Screen capture failed.");
 }

 return (new PImage(desktop));
}


Of course, if there's a better way of doing this, I'd be glad to hear it.

Enjoy!

Re: Capturing the Screen / Taking Screenshots
Reply #1 - Nov 13th, 2009, 1:50pm
 
hey, this is great - exactly what I needed.  Thanks a lot.

I deleted the chunk of code below but what was it for?

Code:
static public void main(String args[]) {  
PApplet.main(new String[] {     "--present", "shooter"  }  );
}
Page Index Toggle Pages: 1