如何用Java中的鼠标指针捕获屏幕图像 [英] How to capture screen image with mouse pointer on it in Java

查看:286
本文介绍了如何用Java中的鼠标指针捕获屏幕图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to capture screen image with mouse pointer on it in Java? In know that i can capture screen with Robot class, but it captures screen without mouse pointer on it, so this is not an solution for me.

推荐答案

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

That's not directly possible, but you can use MouseInfo#getPointerInfo() to get information where the pointer is currently located.

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

获取屏幕截图 BufferedImage 后,你借助Java 2D API,您可以将自己的光标图像放在屏幕截图上的那个位置。

After getting the screenshot as BufferedImage, you can place your own cursor image at exactly that location on the screenshot with help of 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中的鼠标指针捕获屏幕图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆