I have found that java awt can capture the full-screen, but how can I capture the screen of a specific applications? For example, I opened a matlab application and eclipse application. And I cannot tell the size of application screen to the program and just know the matlab is an active window now. And I want to only capture the screen of Matlab. How can I do that?
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.File;
import javax.imageio.ImageIO;
public class Screenshot {
public static final long serialVersionUID = 1L;
public static void main(String[] args)
{
try {
Thread.sleep(120);
Robot r = new Robot();
// It saves screenshot to desired path
String path = "D:// Shot.jpg";
// Used to get ScreenSize and capture image
Rectangle capture =
new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage Image = r.createScreenCapture(capture);
ImageIO.write(Image, "jpg", new File(path));
System.out.println("Screenshot saved");
}
catch (AWTException | IOException | InterruptedException ex) {
System.out.println(ex);
}
}
}
To capture screenshot of a portion of the screen, we need to specify a rectangle region to be captured.the following statements create a capture region which is the first quarter of the screen.
try {
Robot robot = new Robot();
String format = "jpg";
String fileName = "PartialScreenshot." + format;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle captureRect = new Rectangle(0, 0, screenSize.width / 2, screenSize.height / 2);
BufferedImage screenFullImage = robot.createScreenCapture(captureRect);
ImageIO.write(screenFullImage, format, new File(fileName));
System.out.println("A partial screenshot saved!");
} catch (AWTException | IOException ex) {
System.err.println(ex);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.