import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.Queue;
public class Buttons {
public static Rectangle findTextButton(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
boolean[][] visited = new boolean[height][width]; // Verfolgung besuchter Pixel
// Durchsuche das Bild
for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width - 1; x++) {
if (!visited[y][x]) {
// Analysiere, ob Pixel ein potenzieller Startpunkt ist
Color current = new Color(image.getRGB(x, y));
Color next = new Color(image.getRGB(x + 1, y));
if (isHighContrast(current, next)) {
// Flood-Fill, um die Region zu bestimmen
Rectangle bounds = detectButtonBounds(image, x, y, visited);
if (bounds.width > 20 && bounds.height > 10) { // Mindestgröße prüfen
return bounds; // Schaltfläche gefunden
}
}
}
}
}
return null; // Keine Schaltfläche gefunden
}
// Flood-Fill zur Ermittlung der Region
private static Rectangle detectButtonBounds(BufferedImage image, int startX, int startY, boolean[][] visited) {
int width = image.getWidth();
int height = image.getHeight();
// Grenzen der Schaltfläche
int minX = startX, minY = startY, maxX = startX, maxY = startY;
// Warteschlange für die Flood-Fill
Queue<Point> queue = new LinkedList<>();
queue.add(new Point(startX, startY));
visited[startY][startX] = true;
while (!queue.isEmpty()) {
Point current = queue.poll();
int x = current.x;
int y = current.y;
// Aktualisiere die Grenzen
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
// Nachbarn prüfen
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
int newX = x + dx;
int newY = y + dy;
if (newX >= 0 && newY >= 0 && newX < width && newY < height && !visited[newY][newX]) {
Color currentColor = new Color(image.getRGB(x, y));
Color neighborColor = new Color(image.getRGB(newX, newY));
if (isHighContrast(currentColor, neighborColor)) {
queue.add(new Point(newX, newY));
visited[newY][newX] = true;
}
}
}
}
}
// Rechteck mit den gefundenen Grenzen erstellen
return new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
}
private static boolean isHighContrast(Color c1, Color c2) {
int threshold = 50; // Kontrast-Schwellenwert
int rDiff = Math.abs(c1.getRed() - c2.getRed());
int gDiff = Math.abs(c1.getGreen() - c2.getGreen());
int bDiff = Math.abs(c1.getBlue() - c2.getBlue());
return rDiff + gDiff + bDiff > threshold;
}
}