Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One option you might want to consider is to do the processing on multiple threads. One thread can be dedicated to taking screenshots, and many other threads can write to disk. Since writing to disk is not a CPU-intensive operation you can have many of them running concurrently, each one writing to a different files. The following program works fine on my machine with 512M heap size:</p> <pre><code>import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; public class ImageWritingMain { public static void main(String[] args) throws Exception { // a queue final BlockingQueue&lt;BufferedImage&gt; queue = new LinkedBlockingQueue&lt;BufferedImage&gt;(); // schedule a thread to take 20 images per second and put them in // the queue int fps = 20; final ScreenShotRecorder recorder = new ScreenShotRecorder(new Robot(), queue); Timer timer = new Timer(); timer.scheduleAtFixedRate(recorder, 0, (1000L/fps)); // make a directory to hold the screenshot images String id = new Date().toString().replace(' ', '-').replace(':', '-'); File imageDir = new File("images-" + id); imageDir.mkdirs(); // start 10 threads, and each thread reads from the queue and // writes the image to a file int nWriterThreads = 10; ExecutorService threadPool = Executors.newFixedThreadPool(nWriterThreads); for (int i = 0; i &lt; nWriterThreads; i++) { ImageWriter task = new ImageWriter(queue, imageDir); threadPool.submit(task); } System.out.println("Started all threads .."); // wait as long as you want the program to run (1 minute, for example) ... Thread.sleep(60 * 1000L); // .. and shutdown the threads System.out.println("Shutting down all threads"); threadPool.shutdownNow(); timer.cancel(); if (! queue.isEmpty()) { System.out.println("Writing " + queue.size() + " remaining images"); // write the remaining images to disk in the main thread ImageWriter writer = new ImageWriter(queue, imageDir); BufferedImage img = null; while ((img = queue.poll()) != null) { writer.writeImageToFile(img); } } } } class ScreenShotRecorder extends TimerTask { private static final Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); private static final AtomicInteger counter = new AtomicInteger(); private final Robot robot; private final BlockingQueue&lt;BufferedImage&gt; imageQueue; ScreenShotRecorder(Robot robot, BlockingQueue&lt;BufferedImage&gt; imageQueue) { this.robot = robot; this.imageQueue = imageQueue; } @Override public void run() { try { BufferedImage image = robot.createScreenCapture(screenRect); imageQueue.put(image); System.out.println(Thread.currentThread() + ": Took screenshot #" + counter.incrementAndGet()); } catch (InterruptedException e) { System.out.println("Finishing execution of " + Thread.currentThread()); return; } catch (Exception e) { e.printStackTrace(); } } } class ImageWriter implements Runnable { private static final AtomicInteger counter = new AtomicInteger(); private final BlockingQueue&lt;BufferedImage&gt; imageQueue; private final File dir; ImageWriter(BlockingQueue&lt;BufferedImage&gt; imageQueue, File dir) { this.imageQueue = imageQueue; this.dir = dir; } @Override public void run() { while (true) { try { BufferedImage image = imageQueue.take(); writeImageToFile(image); } catch (InterruptedException e) { System.out.println("Finishing execution of " + Thread.currentThread()); return; } catch (Exception e) { e.printStackTrace(); } } } public void writeImageToFile(BufferedImage image) throws IOException { File file = new File(dir, "screenshot-" + counter.incrementAndGet()); ImageIO.write(image, "JPG", file); System.out.println(Thread.currentThread() + ": Wrote " + file.getCanonicalPath()); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload