packagecom.cyd.util;importjava.awt.AWTException;importjava.awt.Graphics2D;importjava.awt.MouseInfo;importjava.awt.Point;importjava.awt.Robot;importjava.awt.Transparency;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.LineNumberReader;importjava.util.HashMap;importjava.util.Map;importjava.util.Map.Entry;importjavax.imageio.ImageIO;public classRobotT1T_2 {static String file1 = "E:\\adb\\1.png";static String file2 = "E:\\adb\\2.png";//始 扫描行,针对不同的手机分辨率可能不一样,需要修改
static int start_y = 300;//按键时长系数,针对不同的手机按键时长可能不一样,需要680.6258244648354的值
static final double time_out = (680.6258244648354 / 563.8129122324177);static boolean istest = false;static boolean is_qz = false;static boolean is_qz_1 = false;static boolean is_qz_2 = false;public static void main(String[] args) throwsAWTException, InterruptedException, IOException {
istest= true;//is_qz=true;//is_qz_1 = true;
is_qz_2 = true;while (true) {
System.out.println("+++++++++++++");if(istest) {
cmd_java("cmd /c start adb.bat ", "E:\\adb\\cofface_adb\\cofface_adb_windows_v5.1");//批处理文件
}
BufferedImage bi= (BufferedImage) ImageIO.read(newFile(file1));
Map map = new HashMap();//获取图像的宽度和高度
int width =bi.getWidth();int height =bi.getHeight();int poix = 0;int poiy = 0;
BufferedImage img= newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d=img.createGraphics();//设置画布为透明
img =g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);//扫描图片
for (int i = start_y; i < height; i++) {for (int j = 0; j < width; j++) {//行扫描
int dip =bi.getRGB(j, i);int p =dip;int red = 0xff & (p >> 16);int green = 0xff & (p >> 8);int blue = 0xff &p;//目标人图形
if (i < (height / 1.5)) {if ((red > 30 && red < 70) && (green > 40 && green < 70) && (blue > 60 && blue < 110)) {//获取小人颜色
int cyd = 6;
RGB rgb_1= dipToRgb(bi, j + cyd, i +cyd);
RGB rgb_2= dipToRgb(bi, j - cyd, i -cyd);
RGB rgb_3= dipToRgb(bi, j - cyd, i +cyd);
RGB rgb_4= dipToRgb(bi, j + cyd, i -cyd);if (istrueren(rgb_1) && istrueren(rgb_2) && istrueren(rgb_3) &&istrueren(rgb_4)) {
img.setRGB(j, i, dip);if (poiy
poix=j;
poiy=i;
}
}
}
}
}
}
}
}for (int rgb_ox = 0; rgb_ox < 10; rgb_ox++) {for (int rgb_ox_y = 0; rgb_ox_y < 10; rgb_ox_y++) {try{
img.setRGB(poix+ rgb_ox, poiy + rgb_ox_y, -2559242);
}catch(Exception e) {
}
}
}int fd_rgb = 10;//颜色差
int qs_rgb_x = 0;int qs_rgb_y = 0;int qs_index = 0;int rgb_oxrgb = -13091994;boolean is_mbh = false;
System.out.println();for (int y = start_y; y < poiy; y++) {//行扫描
RGB rgb_0 = dipToRgb(bi, 0, y);for (int x = 1; x < width; x++) {/***** 去掉目标人颜色*/
int dip =bi.getRGB(x, y);int p =dip;int red = 0xff & (p >> 16);int green = 0xff & (p >> 8);int blue = 0xff &p;//目标人图形
if (y < (height / 1.5)) {if ((red > 30 && red < 70) && (green > 40 && green < 70) && (blue > 60 && blue < 110)) {//获取小人颜色
int cyd = 6;
RGB rgb_1= dipToRgb(bi, x + cyd, y +cyd);
RGB rgb_2= dipToRgb(bi, x - cyd, y -cyd);
RGB rgb_3= dipToRgb(bi, x - cyd, y +cyd);
RGB rgb_4= dipToRgb(bi, x + cyd, y -cyd);if (istrueren(rgb_1) && istrueren(rgb_2) && istrueren(rgb_3) &&istrueren(rgb_4)) {//img.setRGB(x, y, dip);
continue;
}
}
}if (x < (poix + 55) && x > (poix - 55) && y > (poiy - 240))continue;/***** 去掉目标人颜色*/RGB rgb_1=dipToRgb(bi, x, y);
img.setRGB(x, y, bi.getRGB(x, y));if (rgb_cz(rgb_0, rgb_1, fd_rgb)) {//同一种颜色
continue;
}else{
qs_rgb_y=y;if (!is_mbh) {
qs_rgb_x=x;
}
qs_index++;
is_mbh= true;for (int rgb_ox = 0; rgb_ox < 10; rgb_ox++) {for (int rgb_ox_y = 0; rgb_ox_y < 10; rgb_ox_y++) {try{
img.setRGB(x+ rgb_ox, y +rgb_ox_y, rgb_oxrgb);
}catch(Exception e) {
}
}
}
}
}if(is_mbh) {break;
}
}
qs_rgb_x= qs_rgb_x + (qs_index / 2);//System.out.println("目标物坐标:"+qs_rgb_x+" "+qs_rgb_y);
int ox_x =qs_rgb_x;int ox_y = qs_rgb_y + 40;
poiy-= 10;//System.out.println("目标人:" + poix + "\t" + poiy);//System.out.println("中心点:" + ox_x + "\t" + ox_y);
for (int rgb_ox = 0; rgb_ox < 10; rgb_ox++) {for (int rgb_ox_y = 0; rgb_ox_y < 10; rgb_ox_y++) {
img.setRGB(ox_x+ rgb_ox, ox_y +rgb_ox_y, rgb_oxrgb);
}
}int z_i = (poix - ox_x) * (poix - ox_x) + (ox_y - poiy) * (ox_y -poiy);double math_i = (double) Math.sqrt(z_i);
System.out.println("最终距离:" +math_i);
ImageIO.write(img,"png", newFile(file2));double end_time_out = (math_i *time_out);if (math_i < 250) {
end_time_out+= 13;
}
System.out.println("按键时长:" +end_time_out);if(istest) {
cmd_java("cmd /c start adb shell input swipe " + 500 + " " + 1650 + " " + 510 + " " + 1650 + " "
+ (int) end_time_out);
}
Thread.sleep(1000);
}
}/*** 判断小人颜色
*
*@paramrgb
*@return
*/
public static booleanistrueren(RGB rgb) {if (rgb == null) {return true;
}int red =rgb.getRed();int green =rgb.getGreen();int blue =rgb.getBlue();if ((red > 30 && red < 70) && (green > 40 && green < 70) && (blue > 60 && blue < 110)) {//获取小人颜色
return true;
}return false;
}static InputStreamReader ir = null;static LineNumberReader input = null;public static void cmd_java(String cmd) throwsIOException {
Process process= Runtime.getRuntime().exec(cmd, null, new File("C:\\Users\\chenyd\\adb"));
ir= newInputStreamReader(process.getInputStream());
input= newLineNumberReader(ir);while (input.readLine() != null) {
}
input.close();
ir.close();
}public static void cmd_java(String cmd, String url) throwsIOException {
Process process= Runtime.getRuntime().exec(cmd, null, newFile(url));
ir= newInputStreamReader(process.getInputStream());
input= newLineNumberReader(ir);while (input.readLine() != null) {
}
input.close();
ir.close();
}/*** 颜色的差值*/
public static boolean rgb_cz(RGB rgb_1, RGB rgb_2, intfd_rgb) {if (Math.abs(rgb_1.getRed() - rgb_2.getRed()) > fd_rgb || Math.abs(rgb_1.getGreen() - rgb_2.getGreen()) >fd_rgb|| Math.abs(rgb_1.getBlue() - rgb_2.getBlue()) >fd_rgb) {return false;
}return true;
}public static RGB dipToRgb(BufferedImage bi, int j, inti) {try{int dip =bi.getRGB(j, i);int p =dip;int red = 0xff & (p >> 16);int green = 0xff & (p >> 8);int blue = 0xff &p;return newRGB(j, i, red, green, blue);
}catch(Exception e) {
}return null;
}
}classRGB {publicRGB() {
}public RGB(int x, int y, int red, int green, intblue) {super();
X=x;
Y=y;this.red =red;this.green =green;this.blue =blue;
}public intX;public intY;public intred;public intgreen;public intblue;public intgetRed() {returnred;
}public void setRed(intred) {this.red =red;
}public intgetGreen() {returngreen;
}public void setGreen(intgreen) {this.green =green;
}public intgetBlue() {returnblue;
}public void setBlue(intblue) {this.blue =blue;
}public intgetX() {returnX;
}public void setX(intx) {
X=x;
}public intgetY() {returnY;
}public void setY(inty) {
Y=y;
}
}