如何捕获鼠标光标在Java中? [英] How to capture mouse cursor in Java?

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

问题描述

好吧,你们中的大多数会认为这是因为它已经到处写,这不是真的可行,唯一的选择就是使用AWT机器人做的截图重复。

Ok, Most of you will think it's a duplicate because it's already written everywhere that it's not really doable and the only option is to do a screenshot using awt robot.

但是......问题是,鼠标光标甚至没有出现在由AWT机器人产生的图片...我会尝试的gnome-screeshots在那里我可以看到鼠标光标。但是,从Java截图,什么都没有。整个画面,而不是鼠标光标。这就像它在拍摄照片前的hidding它。我搜索,没有运气像setIncludeMouseCursor参数或类似的东西。

But... The issue is that the mouse cursor does not even appear in the picture produced by AWT robot... I tries gnome-screeshots and there I can see the mouse cursor. But from the java screenshot, nothing. The entire picture, but not mouse cursor. It's like it's hidding it before taking the picture. I search for a parameter like setIncludeMouseCursor or anything like that with no luck.

我可以捕捉鼠标的位置,这很好。但是,如果我抓住眼前这个领域,又没有光标。

I can capture the mouse location, that's fine. But if I capture just this area, again no cursor.

不知道如何执行robot.createScreenCapture也捕捉鼠标光标?

Any idea how to enforce robot.createScreenCapture to capture also the mouse cursor?

推荐答案

您需要使用的MouseInfo 类,并使用它的方法静态 getPointerInfo() 获得指针对象重新present你的光标在屏幕上的位置。

You need to use MouseInfo class and use its method static getPointerInfo() to get a Pointer object to represent the position of your cursor on the screen.

一旦你的位置,你可以使用机器人来采取截图作为的BufferedImage 并绘制光标在上面。简单!

Once you have the position, you can use Robot to take a screenshot as a BufferedImage and draw the cursor on it. Simple !

SSCCE

package stack;

import java.awt.AWTException;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.MouseInfo;
import java.awt.PointerInfo;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class GetMousePointer {
    public static void main(String[] args) {
        final String USER_HOME = System.getProperty("user.home");

        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int width = gd.getDisplayMode().getWidth();
        int height = gd.getDisplayMode().getHeight();

        BufferedImage blackSquare = new BufferedImage(50, 50, BufferedImage.TYPE_3BYTE_BGR);
        for(int i = 0; i < blackSquare.getHeight(); i++){
            for(int j = 0; j < blackSquare.getWidth(); j++){
                blackSquare.setRGB(j, i, 128);
            }
        }


        try {
            Robot robot = new Robot();
            BufferedImage screenshot = robot.createScreenCapture(new Rectangle(0,0,width,height));
            PointerInfo pointer = MouseInfo.getPointerInfo();
            int x = (int) pointer.getLocation().getX();
            int y = (int) pointer.getLocation().getY();

            screenshot.getGraphics().drawImage(blackSquare, x, y, null);
            ImageIO.write(screenshot, "PNG", new File(USER_HOME, "screenshot.PNG"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}  

输出结果

Output

蓝色方块的左上角是我的光标的位置。

The top-left corner of the blue square is the position of my cursor.

这篇关于如何捕获鼠标光标在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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