如何使用java捕获其他应用程序的选定屏幕?

我们正在尝试开发一个屏幕捕获实用程序。

我们如何使用Java捕获另一个应用程序的选定屏幕? 我们如何在捕获的屏幕上添加标注?

基于Prajakta对项目的描述 ,我相信操作屏幕截图的一些解释是有序的(我认为John在解释如何使用java.awt.Robot类捕获屏幕截图方面表现非常出色)。 请记住,正如Steve McLeod所说 ,Java可能无法自动定位您想要在屏幕上捕获的窗口的位置。 这很重要,因为Robot类需要自动或手动了解此位置。

当您调用屏幕截图的BufferedImage的createGraphics()方法时,可以通过您收到的Graphics2D对象将标注,文本,图像等添加到屏幕截图中。 我强烈建议您查看Graphics2D的API以更好地了解它的function。 我还建议找一些教程,也许从Sun的2D图形教程开始 。 这本名为“ 肮脏的富客户 ”的书也可能有用。

当您最终要保存此修改过的屏幕截图时,可以使用ImageIO类的“write”方法之一。

这是一个非常简单的,从头到尾的例子。 您需要填写必要的详细信息。

我希望这有点帮助!

Robot robot = new Robot(); // The hard part is knowing WHERE to capture the screen shot from BufferedImage screenShot = robot.createScreenCapture(x, y, width, height); Graphics2D graphics = screenShot.createGraphics(); // Add a label to the screen shot Color textColor = Color.RED; graphics.setColor(textColor); graphics.drawString("Some text", textX, textY); // Save your screen shot with its label ImageIO.save(screenShot, "png", new File("myScreenShot.png")); 
 Robot r = new Robot(); Toolkit t = Toolkit.getDefaultToolkit(); Dimension d = t.getScreenSize(); Image i = r.createScreenCapture( 0, 0, d.width, d.height ); 

应该为您提供整个屏幕的整体图像。 如果你有多个显示器,不确定是否能让你获得全部东西,不过……

也许java.awt.Robot类可以帮助屏幕截图,虽然我不认为它能够定位单个窗口。 至于这些“呼出”,Robot类也可以调用鼠标点击和按键,如果这是你的意思。

也许你可以通过允许用户选择他想要屏幕截图的区域来解决机器人缺乏了解窗口边界的能力。

您有两种方法,如果选项是全屏的,您不必担心,请遵循之前描述的机器人方法。

如果申请适用于所需区域:

  1. 开始所需的应用。

  2. 全屏显示桌面。

  3. 使用屏幕截图作为背景创建一个全屏应用程序,仅允许用户选择捕获图像的区域(从一个小方块开始,让用户拖动,直到他创建所需的屏幕截图)

  4. 将此信息传递给Robot并从该区域截取屏幕截图。

像这样的东西:

alt text http://img136.imageshack.us/img136/8622/screencapturebb3.png

如果您不喜欢使用完整屏幕截图作为背景的选项,则可以使用透明窗口 。

并做同样的事情: – )

Aaahhh的第二个选择是尝试识别分析图像的边界,但老实说,我认为不值得。

还有第三种选择,但这是一个秘密。

您需要提供更具体的信息才能获得有意义的帮助。 首先,需要使用哪些操作系统? 您是否需要捕获单个窗口的内容或字面上的整个显示(您在原始post中使用了模糊术语“其他应用程序的选定屏幕”)。 当您“将标注添加到捕获的屏幕”时,您希望看到什么?

如果您想截取另一个非Java应用程序代码的特定窗口的屏幕截图,我认为您必须编写一些本机(即非Java)代码。 Java应用程序与非Java应用程序之间的交互很困难。

使用此代码,我可以在windows10中制作某些窗口的屏幕,不要忘记依赖。

积分转到: Windows:如何获取所有可见窗口的列表?

  net.java.dev.jna jna 4.5.0  

码:

 import java.awt.AWTException; import java.awt.Rectangle; import java.awt.Robot; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import javax.imageio.ImageIO; import com.sun.jna.Native; import com.sun.jna.Structure; import com.sun.jna.win32.StdCallLibrary; public class Main { public static void main(String[] args) throws AWTException, IOException { int hWnd = User32.instance.FindWindowA(null, "Minesweeper X"); WindowInfo w = getWindowInfo(hWnd); User32.instance.SetForegroundWindow(w.hwnd); BufferedImage createScreenCapture = new Robot().createScreenCapture(new Rectangle(w.rect.left, w.rect.top, w.rect.right - w.rect.left, w.rect.bottom - w.rect.top)); ImageIO.write(createScreenCapture, "png", new File("screen.png")); // listAllWindows(); } private static void listAllWindows() throws AWTException, IOException { final List inflList = new ArrayList(); final List order = new ArrayList(); int top = User32.instance.GetTopWindow(0); while (top != 0) { order.add(top); top = User32.instance.GetWindow(top, User32.GW_HWNDNEXT); } User32.instance.EnumWindows(new WndEnumProc() { public boolean callback(int hWnd, int lParam) { WindowInfo info = getWindowInfo(hWnd); inflList.add(info); return true; } }, 0); Collections.sort(inflList, new Comparator() { public int compare(WindowInfo o1, WindowInfo o2) { return order.indexOf(o1.hwnd) - order.indexOf(o2.hwnd); } }); for (WindowInfo w : inflList) { System.out.println(w); } } public static WindowInfo getWindowInfo(int hWnd) { RECT r = new RECT(); User32.instance.GetWindowRect(hWnd, r); byte[] buffer = new byte[1024]; User32.instance.GetWindowTextA(hWnd, buffer, buffer.length); String title = Native.toString(buffer); WindowInfo info = new WindowInfo(hWnd, r, title); return info; } public static interface WndEnumProc extends StdCallLibrary.StdCallCallback { boolean callback(int hWnd, int lParam); } public static interface User32 extends StdCallLibrary { public static final String SHELL_TRAY_WND = "Shell_TrayWnd"; public static final int WM_COMMAND = 0x111; public static final int MIN_ALL = 0x1a3; public static final int MIN_ALL_UNDO = 0x1a0; final User32 instance = (User32) Native.loadLibrary("user32", User32.class); boolean EnumWindows(WndEnumProc wndenumproc, int lParam); boolean IsWindowVisible(int hWnd); int GetWindowRect(int hWnd, RECT r); void GetWindowTextA(int hWnd, byte[] buffer, int buflen); int GetTopWindow(int hWnd); int GetWindow(int hWnd, int flag); boolean ShowWindow(int hWnd); boolean BringWindowToTop(int hWnd); int GetActiveWindow(); boolean SetForegroundWindow(int hWnd); int FindWindowA(String winClass, String title); long SendMessageA(int hWnd, int msg, int num1, int num2); final int GW_HWNDNEXT = 2; } public static class RECT extends Structure { public int left, top, right, bottom; @Override protected List getFieldOrder() { List order = new ArrayList<>(); order.add("left"); order.add("top"); order.add("right"); order.add("bottom"); return order; } } public static class WindowInfo { int hwnd; RECT rect; String title; public WindowInfo(int hwnd, RECT rect, String title) { this.hwnd = hwnd; this.rect = rect; this.title = title; } public String toString() { return String.format("(%d,%d)-(%d,%d) : \"%s\"", rect.left, rect.top, rect.right, rect.bottom, title); } } }