简体   繁体   English

Java 获取像素 LIVE 的颜色

[英]Java get color of pixel LIVE

I have a problem with finding the current color under the cursor.在 cursor 下查找当前颜色时遇到问题。

My code:我的代码:

import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;

public class Test {
    public static void main(String[] args) throws Exception {
        PointerInfo pointer;
        pointer = MouseInfo.getPointerInfo();
        Point coord = pointer.getLocation();

        Robot robot = new Robot();
        robot.delay(2000);

        while(true) {
            coord = MouseInfo.getPointerInfo().getLocation();       
            Color color = robot.getPixelColor((int)coord.getX(), (int)coord.getX());
            if(color.getGreen() == 255 && color.getBlue() == 255 && color.getRed() == 255) {
                System.out.println("WHITE FOUND");
            }
            robot.delay(1000);
        }
    }
}

When I run it, even when I hold my mouse on the gray area, I am getting “WHITE FOUND WHITE FOUND” message.当我运行它时,即使我将鼠标放在灰色区域上,我也会收到“WHITE FOUND WHITE FOUND”消息。

What can be the problem?可能是什么问题? Can you guys test if it does not work for you also?你们能测试一下它是否也对你有用吗?

Added Picture: I am holding my cursor on Eclipse gray area but getting “WHITE FOUND” message.添加图片:我在 Eclipse 灰色区域上拿着我的 cursor 但收到“发现白色”消息。

在此处输入图像描述

I think the problem is you are using getX twice instead of getX and getY我认为问题是您使用 getX 两次而不是 getX 和 getY

Color color = robot.getPixelColor((int)coord.getX(), (int)coord.getX())

Should be应该

Color color = robot.getPixelColor((int)coord.getX(), (int)coord.getY())

You're using getX() twice.您使用 getX() 两次。 [min length] [最小长度]

You might also like Zoom , which uses the related method createScreenCapture() to collect screen pixels en masse and display color information about each in a tool tip.您可能还喜欢Zoom ,它使用相关方法createScreenCapture()集体收集屏幕像素并在工具提示中显示有关每个像素的颜色信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM