gpt4 book ai didi

MacOS中某App的Java截图

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:44:52 25 4
gpt4 key购买 nike

我想在我的 MacOS 中截取某些应用程序的屏幕截图,即使是在另一个虚拟屏幕上而不是在 Activity 屏幕上也是如此。

我可以使用以下代码进行主动屏幕捕获,但如何捕获给定的应用程序?

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();
}
}
}
}

最佳答案

尝试使用/usr/sbin/screencapture,并用

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

这里是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

我认为带窗口 id 的 -w 选项是可以使用的。但是要获取窗口 ID,您可能需要另一个实用程序。一个这样的实用程序是 pyscreencapture .否则,我确信谷歌搜索如何获取窗口 ID 会导致更多获取所需窗口 ID 的方法。

虽然我不完全确定你所说的虚拟屏幕是什么意思,而且你可能在获取窗口 ID 或捕获屏幕时遇到问题。

关于MacOS中某App的Java截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29489584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com