package de.tutorials;
import java.awt.AWTException;
import java.awt.DisplayMode;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import static java.util.concurrent.TimeUnit.SECONDS;
public class FastScreenShooterExample {
public static void main(String[] args) throws Exception {
try (ScreenGrabber sg = new AWTRobotScreenGrabber()) {
int benchmarkPeriodInSeconds = 10;
long endTime = System.currentTimeMillis() + SECONDS.toMillis(benchmarkPeriodInSeconds);
int grabs = 0;
while (System.currentTimeMillis() < endTime) {
sg.grabAndOutput("d:/temp/screens", "screen" + grabs);
grabs++;
}
System.out.printf("%s grabs/s", grabs / benchmarkPeriodInSeconds).println();
}
}
static interface ScreenGrabber extends Closeable {
void grabAndOutput(String path, String imageName);
}
static abstract class AbstractScreenGrabber implements ScreenGrabber {
protected Rectangle screenRect;
protected ExecutorService imageWriterService;
public AbstractScreenGrabber() {
DisplayMode displayMode = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
this.screenRect = new Rectangle(0, 0, displayMode.getWidth(),displayMode.getHeight());
this.imageWriterService = Executors.newSingleThreadExecutor();
}
@Override
public void grabAndOutput(String path, String imageName) {
BufferedImage screenshot = grab();
write(screenshot, path, imageName);
}
protected abstract BufferedImage grab();
private void write(BufferedImage screenshot, String path, String imageName) {
imageWriterService.submit(new WriteImageCommand(screenshot, path,imageName));
}
@Override
public void close() throws IOException {
imageWriterService.shutdown();
}
}
static class AWTRobotScreenGrabber extends AbstractScreenGrabber {
private Robot robot;
public AWTRobotScreenGrabber() {
try {
this.robot = new Robot();
} catch (AWTException e) {
throw new RuntimeException(e);
}
}
@Override
public BufferedImage grab() {
return grab(this.screenRect);
}
public BufferedImage grab(Rectangle screenRect) {
return robot.createScreenCapture(screenRect);
}
}
static class WriteImageCommand implements Runnable {
private final BufferedImage screenshot;
private final String path;
private final String imageName;
public WriteImageCommand(BufferedImage screenshot, String path,String imageName) {
this.screenshot = screenshot;
this.path = path;
this.imageName = imageName;
}
@Override
public void run() {
try (BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(new File(path, imageName + ".jpg")))) {
//you have to disable the error for restricted / deprecated api ...
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bout);
encoder.encode(screenshot);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}