Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java screenshot of certain App in MacOS

I want to take screenshot of certain app in my MacOS, even if on another virtual screen and not in active screen.

I can take active screen capture with following code but how to capture given application?

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;

public class Screenshot {
    public static void main(String args[]) throws AWTException, IOException {
        while(true) {
            String timeStamp = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());

            BufferedImage screencapture = new Robot().createScreenCapture(
                new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())
            );

            // Save as JPEG
            File file = new File("./screens/screencapture" + timeStamp + ".jpg");
            ImageIO.write(screencapture, "jpg", file);

            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
like image 276
Szymon Toda Avatar asked Apr 07 '15 10:04

Szymon Toda


1 Answers

Try using /usr/sbin/screencapture, and calling it with

Runtime.getRuntime().exec("/usr/sbin/screencapture");

Here is the usage output of screencapture

usage: screencapture [-icMPmwsWxSCUtoa] [files]
  -c         force screen capture to go to the clipboard
  -C         capture the cursor as well as the screen. only in non-interactive modes
  -d         display errors to the user graphically
  -i         capture screen interactively, by selection or window
               control key - causes screen shot to go to clipboard
               space key   - toggle between mouse selection and
                             window selection modes
               escape key  - cancels interactive screen shot
  -m         only capture the main monitor, undefined if -i is set
  -M         screen capture output will go to a new Mail message
  -o         in window capture mode, do not capture the shadow of the window
  -P         screen capture output will open in Preview
  -s         only allow mouse selection mode
  -S         in window capture mode, capture the screen not the window
  -t<format> image format to create, default is png (other options include pdf, jpg, tiff and other formats)
  -T<seconds> Take the picture after a delay of <seconds>, default is 5
  -w         only allow window selection mode
  -W         start interaction in window selection mode
  -x         do not play sounds
  -a         do not include windows attached to selected windows
  -r         do not add dpi meta data to image
  -l<windowid> capture this windowsid
  -R<x,y,w,h> capture screen rect
  files   where to save the screen capture, 1 file per screen

I think the -w option with window id is the one to use. But to get the window id, you will probably need another utility. One such utility is pyscreencapture. Otherwise I am sure googling how to get the window id will result in more ways to get the window id you require.

Though I am not entirely sure what you mean by virtual screen, and you may have problem with obtaining the window id or capturing the screen.

like image 151
Sanj Avatar answered Oct 29 '22 11:10

Sanj