如何用Java中的鼠标指针捕获屏幕图像

如何在Java中用鼠标指针捕获屏幕图像? 知道我可以使用Robot类捕获屏幕,但它捕获屏幕而没有鼠标指针,所以这对我来说不是一个解决方案。

这不是直接可能的,但您可以使用MouseInfo#getPointerInfo()来获取指针当前所在的位置的信息。

 int x = MouseInfo.getPointerInfo().getLocation().x; int y = MouseInfo.getPointerInfo().getLocation().y; 

在将屏幕截图设置为BufferedImage ,您可以借助Java 2D API将自己的光标图像放在屏幕截图的确切位置。

 Rectangle screen = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); BufferedImage screenCapture = new Robot().createScreenCapture(screen); Image cursor = ImageIO.read(new File("c:/cursor.gif")); int x = MouseInfo.getPointerInfo().getLocation().x; int y = MouseInfo.getPointerInfo().getLocation().y; Graphics2D graphics2D = screenCapture.createGraphics(); graphics2D.drawImage(cursor, x, y, 16, 16, null); // cursor.gif is 16x16 size. ImageIO.write(screenCapture, "GIF", new File("c:/capture.gif")); 

您可以使用Java Native Access动态访问特定于操作系统的鼠标光标/指针。