Java Pixelbot (и переместите мышь в эту точку) - PullRequest
0 голосов
/ 14 апреля 2020
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;
    }
}

Я хочу найти определенный c Цвет пикселя (желтый) в указанной c области на экране (например, поле зрения), а затем переместить мышь с помощью r.mouseMove (); на эту конкретную позицию. Может кто-нибудь изменить код, чтобы он работал?

1 Ответ

0 голосов
/ 15 апреля 2020

Здесь проверьте код ниже. У вас было две ошибки проверки комментариев.

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 () .

...