import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;



public class main {
public static Point pixelSearchArea(String hexColor, Point topLeft, Point bottomRight) throws AWTException{

        int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
        int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;

        int areaWidth = bottomRight.y - topLeft.y;
        int areaHeight = bottomRight.y - topLeft.y;

        Rectangle area = new Rectangle(topLeft.x, topLeft.y, areaWidth, areaHeight);

        Color colorColor = Color.decode(hexColor);

        int searchColor = colorColor.getRGB();

        Robot r = new Robot();
        Rectangle screenRect = area;
        BufferedImage bimage = r.createScreenCapture(screenRect);
        int width = bimage.getWidth();
        int height = bimage.getHeight();
        int[] colors = new int[width * height];
        int[] all = bimage.getRGB(0, 0, width, height, colors , 0, width);

        for(int i = 0; i < all.length; i++)
        {
            if (all[i] == searchColor)
            {
                int y = ((i + 1) / width);
                int x = (i) - (y * width);
                return new Point(screenWidth - (screenWidth - topLeft.x) + x,screenHeight - (screenHeight - topLeft.y) + y);
            }
        }

    return null;
    }
}


我想在屏幕上的特定区域(如FOV)上搜索特定的像素颜色(黄色),然后使用r.mouseMove()移动鼠标;到这个特定位置。
有人可以更改密码使其生效吗?

最佳答案

在这里签出下面的代码。
您有两个错误检查注释。

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;



public class App {

    public static Point pixelSearchArea(String hexColor, Point topLeft, Point bottomRight) throws AWTException{

        int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
        int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;

        // int areaWidth = bottomRight.y - topLeft.y; - for width use X
        int areaWidth = bottomRight.x - topLeft.x;
        int areaHeight = bottomRight.y - topLeft.y;

        Rectangle area = new Rectangle(topLeft.x, topLeft.y, areaWidth, areaHeight);

        Color colorColor = Color.decode(hexColor);

        int searchColor = colorColor.getRGB();

        Robot r = new Robot();
        Rectangle screenRect = area;
        BufferedImage bimage = r.createScreenCapture(screenRect);
        int width = bimage.getWidth();
        int height = bimage.getHeight();

        // int[] colors = new int[width * height]; - read the docs.
        int[] all = bimage.getRGB(0, 0, width, height, null, 0, width);

        for(int i = 0; i < all.length; i++)
        {
            if (all[i] == searchColor)
            {
                int y = ((i + 1) / width);
                int x = (i) - (y * width);
                return new Point(screenWidth - (screenWidth - topLeft.x) + x,screenHeight - (screenHeight - topLeft.y) + y);
            }
        }

    return null;
    }

    public static void main(String[] args) {
        try {
            // It will set the mouse pointer to the last white pixel
            Point p = pixelSearchArea("#FFFFFF", new Point(0, 0), new Point(500, 500));
            Robot r = new Robot();
            r.mouseMove(p.x, p.y);
        } catch (AWTException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


阅读BufferedImage#getRGB()的文档。

10-08 01:50