Szy*_*oda 8 java macos screenshot
我想在我的MacOS中截取某些应用程序的屏幕截图,即使在另一个虚拟屏幕上也没有在活动屏幕中.
我可以使用以下代码进行活动屏幕捕获,但如何捕获给定的应用程序?
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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
尝试使用/ usr/sbin/screencapture,然后调用它
Runtime.getRuntime().exec("/usr/sbin/screencapture");
Run Code Online (Sandbox Code Playgroud)
这是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
Run Code Online (Sandbox Code Playgroud)
我认为带窗口id的-w选项是要使用的选项.但要获得窗口ID,您可能需要另一个实用程序.一种这样的效用是pyscreencapture.否则我肯定谷歌搜索如何获取窗口ID将导致更多的方法来获取您需要的窗口ID.
虽然我不完全确定您的虚拟屏幕是什么意思,但您可能在获取窗口ID或捕获屏幕时遇到问题.