package Main;
import java.awt.Color;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class Main {
private double v = 0.7075;
public static void main(String[] args) throws Exception {
File d = new File("./");
File[] ds = d.listFiles();
for (File tmp : ds) {
if (tmp.getName().endsWith("png")) {
tmp.delete();
}
}
Main m = new Main();
while (true) {
try {
m.getImage();
m.load();
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
m.push(10.0);
}
}
}
private void getImage() throws Exception {
Thread.sleep(1200);
Process p = Runtime.getRuntime().exec("adb shell screencap -p /sdcard/1.png");
p.waitFor();
p = Runtime.getRuntime().exec("adb pull /sdcard/1.png .");
p.waitFor();
}
private void push(double time) throws Exception {
String exe = "adb shell input swipe 410 1720 410 1720 " + (int) time;
Runtime.getRuntime().exec(exe);
System.out.println(exe);
Thread.sleep((long) (time + 720));
}
private int index = 0;
private Color b = null;
private String genFileName() {
return "P_" + (index++) + ".png";
}
void mark(Point p, BufferedImage buff) {
for (int y = -10; y < 10; y++) {
for (int x = -10; x < 10; x++) {
buff.setRGB((int) (x + p.getX()), (int) (y + p.getY()), Color.RED.getRGB());
}
}
}
void setBackground(BufferedImage buff) {
b = new Color(buff.getRGB(500, 500));
System.out.println("background RGB:" + b);
}
private void load() throws Exception {
BufferedImage buff = ImageIO.read(new File("./1.png"));
setBackground(buff);
Point t = findTarget(buff), me = findMe(buff);
System.out.println("target:" + t + " me:" + me);
mark(t, buff);
mark(me, buff);
ImageIO.write(buff, "png", new File(genFileName()));
double dis = Math.sqrt(
(me.getX() - t.getX()) * (me.getX() - t.getX()) + (me.getY() - t.getY()) * (me.getY() - t.getY()));
double time = dis / v;
if (dis < 300) {
System.out.println(String.format("distance plus from [%f] to [%f]-------------------", time, time + 100));
time += 50;
}
push(time);
}
private boolean colorSame(int left, int mid, int right) {
return colorSame(left, right) && colorSame(left, mid) && colorSame(mid, right);
}
private boolean colorSame(int left, int right) {
Color l = new Color(left);
Color r = new Color(right);
return Math.abs(l.getRed() - r.getRed()) < 10 && Math.abs(l.getGreen() - r.getGreen()) < 10
&& Math.abs(l.getBlue() - r.getBlue()) < 10;
}
public Point findTarget(BufferedImage buff) {
int width = buff.getWidth();
int step = 75;
for (int y = 500; y < 1743; y++) {
for (int x = 10; x < width - step - 1; x++) {
int left = buff.getRGB(x, y);
int right = buff.getRGB(x + step, y);
int mid = buff.getRGB(x + step / 2, y);
if (colorSame(left, mid, right) && !isBackGround(left) && !isBackGround(right) && !isBackGround(mid)) {
// isBackGround(left);
// isBackGround(left);
return new Point(x + step / 2, y /*+ step * 1 / 2*/);
}
}
}
return null;
}
private boolean isBackGround(int rgb) {
if (b == null) {
int gray = this.gray(rgb);
return gray < 235 && gray > 225;
} else {
Color t = new Color(rgb);
return Math.abs(t.getRed() - b.getRed()) < 20 && Math.abs(t.getGreen() - b.getGreen()) < 20
&& Math.abs(t.getBlue() - b.getBlue()) < 20;
}
}
int gray(int rgb) {
Color c = new Color(rgb);
return (c.getRed() + c.getGreen() + c.getBlue()) / 3;
}
private Point findMe(BufferedImage buff) {
int width = buff.getWidth(), height = buff.getHeight();
for (int y = 700; y < 1743; y++) {
for (int x = 10; x < width - 70 - 1; x++) {
int left = buff.getRGB(x, y);
int right = buff.getRGB(x + 70, y);
if (isMe(left) && isMe(right) && !isBackGround(left) && !isBackGround(right)) {
return new Point(x + 35, y);
}
}
}
return new Point(width, height);
}
private boolean isMe(int rgb) {
Color a = new Color(rgb);
return a.getRed() > 39 && a.getRed() < 60 && a.getGreen() > 39 && a.getGreen() < 60 && a.getBlue() > 70
&& a.getBlue() < 98;
}
}