Skip to content

Commit

Permalink
Merge pull request #48 from bambootang/update-0.0.5
Browse files Browse the repository at this point in the history
Update 0.0.5
  • Loading branch information
easyworld authored Jan 2, 2018
2 parents 1f3803c + 09a4d2c commit b381e4d
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 87 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ adb pull /sdcard/screen.png # 下载截屏文件到本地
```

## 使用方法
**当前最新代码版本为0.0.4**
**当前最新代码版本为0.0.5**
1. 在电脑上下载好adb,并安装JDK7及以上版本以及配置好java环境变量
2. 打开安卓手机的usb调试模式并授权连接的电脑
> 如果是小米手机,在USB调试下方有``USB调试(安全设置)``打开允许模拟点击 感谢[@wotermelon](https://github.com/wotermelon)
Expand Down Expand Up @@ -42,7 +42,7 @@ java -jar playJumpJumpWithMouse.jar -a "C:\Users\Home\Desktop\platform-tools\adb
* 全自动模式(auto-mode):也就是挂机模式,不需要人工操作,启动后既可以自动识别算法自动帮你玩跳一跳.

## 一些需要注意的点
* 有些安卓机并不能很好的使用adb exec-out,导致截屏异常,可以尝试使用[老代码](https://github.com/easyworld/PlayJumpJumpWithMouse/blob/0ba11f1968db2a46660562f1b27d3925eec1b9ce/src/com/company/AdbCaller.java)替换现有方法
* 默认分辨率比例为:**675x1200**,如果你的手机分辨率不是该比例则必须设置-s参数后手动和半自动才能计算准确。比如mate10 pro的分辨率是1080x2160,则设置为**-s 675x1350**.

## 运行截图
![这是一个截图](https://github.com/easyworld/PlayJumpJumpWithMouse/raw/master/screenshot.png)
Expand Down
Binary file added playJumpJumpWithMouse-0.0.5.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

<groupId>com.company</groupId>
<artifactId>playJumpJumpWithMouse</artifactId>
<version>0.0.4</version>
<version>0.0.5</version>
<packaging>jar</packaging>

<name>playJumpJumpWithMouse</name>
<url>http://maven.apache.org</url>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand Down
157 changes: 105 additions & 52 deletions src/main/java/com/company/playJumpJumpWithMouse/AdbCaller.java
Original file line number Diff line number Diff line change
@@ -1,67 +1,120 @@
package com.company.playJumpJumpWithMouse;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class AdbCaller {

private static String adbPath = Constants.ADB_PATH;
private static String adbPath = Constants.ADB_PATH;

private static String screenshotLocation = Constants.SCREENSHOT_LOCATION;
private static String screenshotLocation = Constants.SCREENSHOT_LOCATION;

public static void setAdbPath(String adbPath) {
AdbCaller.adbPath = adbPath;
}
private static Boolean error = null;

public static void setScreenshotLocation(String screenshotLocation) {
AdbCaller.screenshotLocation = screenshotLocation;
}
public static void setAdbPath(String adbPath) {
AdbCaller.adbPath = adbPath;
}

/**
* 调用adb长按屏幕
*
* @param timeMilli
*/
public static void longPress(double timeMilli) {
try {
Process process = Runtime.getRuntime()
.exec(adbPath + " shell input touchscreen swipe 170 187 170 187 " + (int) timeMilli);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = bufferedReader.readLine()) != null)
System.out.println(s);
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void setScreenshotLocation(String screenshotLocation) {
AdbCaller.screenshotLocation = screenshotLocation;
}

/**
* 改进的截图方法<br>
* 感谢 hxzqlh
*/
public static void printScreen() {
/**
* 调用adb长按屏幕
*
* @param timeMilli
*/
public static void longPress(double timeMilli, BufferedImage image) {
try {
int width = image.getWidth() / 3 + (int) (Math.random() * image.getWidth() / 3);
int height = image.getHeight() - 300 + (int) (Math.random() * 200);
Process process = Runtime.getRuntime()
.exec(adbPath + " shell input touchscreen swipe " + width + " " + height + " " + width + " " + height + " " + (int) timeMilli);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = bufferedReader.readLine()) != null)
System.out.println(s);
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

try {
String[] args = new String[] { "bash", "-c", adbPath + " exec-out screencap -p > " + screenshotLocation };
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("win")) {
args[0] = "cmd";
args[1] = "/c";
}
Process p1 = Runtime.getRuntime().exec(args);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p1.getErrorStream()));
String s;
while ((s = bufferedReader.readLine()) != null)
System.out.println(s);
p1.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 改进的截图方法<br>
* 感谢 hxzqlh
* 当改进的截图方法不能正常执行时降级为常规方法
*/
public static void printScreen() {
if (error != null && error) {
printScreenWithOld();
} else {
try {
String[] args = new String[]{"bash", "-c", adbPath + " exec-out screencap -p > " + screenshotLocation};
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("win")) {
args[0] = "cmd";
args[1] = "/c";
}
Process p1 = Runtime.getRuntime().exec(args);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p1.getErrorStream()));
String s;
while ((s = bufferedReader.readLine()) != null)
System.out.println(s);
p1.waitFor();
checkScreenSuccess();
} catch (IOException e) {
e.printStackTrace();
error = true;
printScreenWithOld();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

private static void checkScreenSuccess() throws IOException {
if (error == null) {
BufferedImage image = ImageIO.read(new File(screenshotLocation));
if (image == null) {
throw new IOException("cann't read file \"" + screenshotLocation + "\" into image object");
}
}
}

public static void printScreenWithOld() {
try {
Process p1 = Runtime.getRuntime().exec(adbPath + " shell screencap -p /sdcard/screenshot.png");
p1.waitFor();
Process p2 = Runtime.getRuntime().exec(adbPath + " pull /sdcard/screenshot.png " + screenshotLocation);
p2.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public static int getSize() {
try {
Process p = Runtime.getRuntime().exec("adb shell wm density");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = bufferedReader.readLine();
String[] splis = line.split(" ");
return Integer.valueOf(splis[splis.length - 1]);
} catch (Exception e) {
e.printStackTrace();
}
return 480;
}

public static void main(String[] args) {
System.out.println(getSize());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class BackgroundImage4Panel extends javax.swing.JFrame {

private static int playMode = Constants.MODE_MANUAL;

private static BufferedImage bufferedImage;

/**
* Creates new form NewJFrame
*/
Expand All @@ -52,6 +54,8 @@ public BackgroundImage4Panel() {
*/
public static void main(String[] args) {

ScreenAdapter.SCREEN_DPI = AdbCaller.getSize();

final int resizedScreenWidth, resizedScreenHeight;
final double resizedDistancePressTimeRatio;
final int screenshotInterval;
Expand Down Expand Up @@ -158,7 +162,7 @@ private static void manualMode(final int resizedScreenWidth, final int resizedSc
protected void paintComponent(Graphics g) {
super.paintComponent(g);
try {
BufferedImage bufferedImage = ImageIO.read(new File(screenshotPath));
bufferedImage = ImageIO.read(new File(screenshotPath));
BufferedImage newImage = new BufferedImage(resizedScreenWidth, resizedScreenHeight,
bufferedImage.getType());
if (playMode == Constants.MODE_SEMI_AUTO) {
Expand Down Expand Up @@ -196,7 +200,7 @@ public void mouseClicked(MouseEvent e) {
int distance = distance(firstPoint, secondPoint);
System.out.println("distance:" + distance);
isFirst = true;
AdbCaller.longPress(distance * resizedDistancePressTimeRatio);// magic
AdbCaller.longPress(distance * resizedDistancePressTimeRatio, bufferedImage);// magic
// number
try {
Thread.sleep(screenshotInterval);// wait for screencap
Expand Down Expand Up @@ -264,7 +268,7 @@ public void run() {
+ "] , secondPoint = [x=" + secondPoint.x + ",y=" + secondPoint.y + "]");
ColorFilterFinder.updateLastShapeMinMax(bufferedImage, firstPoint, secondPoint);
distance = distance(firstPoint, secondPoint);
AdbCaller.longPress(distance * resizedDistancePressTimeRatio);// magic
AdbCaller.longPress(distance * resizedDistancePressTimeRatio, bufferedImage);// magic
// number
try {
Thread.sleep(screenshotInterval);// wait for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public class ColorFilterFinder {

static Point startCenterPoint;

static int lastShapeMinMax = 150;
static int lastShapeMinMax = ScreenAdapter.getShapeMinWidth();

public static Point findEndCenter(BufferedImage bufferedImage, Point startCenterPoint) {
ColorFilterFinder.startCenterPoint = startCenterPoint;
bgColor = new Color(bufferedImage.getRGB(540, 700));
bgColor = new Color(bufferedImage.getRGB(bufferedImage.getWidth() / 2, 120));

Point tmpStartCenterPoint;
Point tmpEndCenterPoint;
Expand All @@ -29,7 +29,7 @@ public static Point findEndCenter(BufferedImage bufferedImage, Point startCenter
(int) startCenterPoint.getY());

Color lastColor = bgColor;
for (int y = 600; y < startCenterPoint.y; y++) {
for (int y = bufferedImage.getWidth() / 3; y < startCenterPoint.y; y++) {
for (int x = 10; x < bufferedImage.getWidth(); x++) {
if (rectangle.contains(x, y)) {
continue;
Expand All @@ -38,9 +38,9 @@ public static Point findEndCenter(BufferedImage bufferedImage, Point startCenter
if ((Math.abs(newColor.getRed() - lastColor.getRed())
+ Math.abs(newColor.getBlue() - lastColor.getBlue())
+ Math.abs(newColor.getGreen() - lastColor.getGreen()) >= 20)
|| (Math.abs(newColor.getRed() - lastColor.getRed()) >= 15
|| Math.abs(newColor.getBlue() - lastColor.getBlue()) >= 15
|| Math.abs(newColor.getGreen() - lastColor.getGreen()) >= 15)) {
|| (Math.abs(newColor.getRed() - lastColor.getRed()) >= 10
|| Math.abs(newColor.getBlue() - lastColor.getBlue()) >= 10
|| Math.abs(newColor.getGreen() - lastColor.getGreen()) >= 10)) {
// System.out.println(BufferImageTest.toHexFromColor(newColor));
// System.out.println(BufferImageTest.toHexFromColor(lastColor));
// System.out.println("y = " + y + " x = " + x);
Expand All @@ -49,6 +49,7 @@ public static Point findEndCenter(BufferedImage bufferedImage, Point startCenter
tmpEndCenterPoint = findEndCenterPoint(bufferedImage, tmpStartCenterPoint);
return new Point(tmpStartCenterPoint.x, (tmpEndCenterPoint.y + tmpStartCenterPoint.y) / 2);
}
lastColor = newColor;
}
}
return null;
Expand All @@ -74,11 +75,11 @@ private static Point findEndCenterPoint(BufferedImage bufferedImage, Point tmpSt
centY = i;
}
}
if (centY - tmpStartCenterPoint.y < 40) {
centY = centY + 40;
if (centY - tmpStartCenterPoint.y < ScreenAdapter.getMinShapeHeight()) {
centY = centY + ScreenAdapter.getMinShapeHeight();
}
if (centY - tmpStartCenterPoint.y > 230) {
centY = tmpStartCenterPoint.y + 230;
if (centY - tmpStartCenterPoint.y > ScreenAdapter.getMaxShapeHeight()) {
centY = tmpStartCenterPoint.y + ScreenAdapter.getMaxShapeHeight();
}
return new Point(centX, centY);
}
Expand All @@ -92,8 +93,8 @@ private static Point findStartCenterPoint(BufferedImage bufferedImage, int x, in
if ((Math.abs(newColor.getRed() - lastColor.getRed()) + Math.abs(newColor.getBlue() - lastColor.getBlue())
+ Math.abs(newColor.getGreen() - lastColor.getGreen()) >= 20)
|| (Math.abs(newColor.getRed() - lastColor.getRed()) >= 15
|| Math.abs(newColor.getBlue() - lastColor.getBlue()) >= 15
|| Math.abs(newColor.getGreen() - lastColor.getGreen()) >= 15)) {
|| Math.abs(newColor.getBlue() - lastColor.getBlue()) >= 15
|| Math.abs(newColor.getGreen() - lastColor.getGreen()) >= 15)) {
centX = x + (i - x) / 2;
} else {
break;
Expand All @@ -106,23 +107,23 @@ private static boolean like(Color a, Color b) {
return !((Math.abs(a.getRed() - b.getRed()) + Math.abs(a.getBlue() - b.getBlue())
+ Math.abs(a.getGreen() - b.getGreen()) >= 20)
|| (Math.abs(a.getRed() - b.getRed()) >= 15 || Math.abs(a.getBlue() - b.getBlue()) >= 15
|| Math.abs(a.getGreen() - b.getGreen()) >= 15));
|| Math.abs(a.getGreen() - b.getGreen()) >= 15));
}

public static void updateLastShapeMinMax(BufferedImage bufferedImage, Point first, Point second) {
if (first.x < second.y) {
for (int x = second.x; x < bufferedImage.getWidth(); x++) {
Color newColor = new Color(bufferedImage.getRGB(x, second.y));
if (like(newColor, bgColor)) {
lastShapeMinMax = (int) Math.max((x - second.x) * 1.5, 150);
lastShapeMinMax = (int) Math.max((x - second.x) * 1.5, lastShapeMinMax);
break;
}
}
} else {
for (int x = second.x; x >= 10; x--) {
Color newColor = new Color(bufferedImage.getRGB(x, second.y));
if (like(newColor, bgColor)) {
lastShapeMinMax = (int) Math.max((second.x - x) * 1.5, 150);
lastShapeMinMax = (int) Math.max((second.x - x) * 1.5, lastShapeMinMax);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public class Constants {
*/
public static final int SCREENSHOT_INTERVAL = 3000; // ms

public static final int HDPI = 320;

public static final int XHDPI = 480;

public static final int XXHDPI = 640;

/**
* 手动模式
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static Point findEndCenter(BufferedImage bufferedImage, Point startCenter
int centerX = 0;
int centerY = 0;
int height = bufferedImage.getHeight() * 2 / 3;
for (int h = 200; h < height && h < startCenterPoint.y; h++) {
for (int h = bufferedImage.getHeight() / 3; h < height && h < startCenterPoint.y; h++) {
for (int w = 0; w < width; w++) {
int color = bufferedImage.getRGB(w, h);
Color newColor = new Color(color);
Expand Down Expand Up @@ -74,7 +74,7 @@ static Point findWhiteCenter(BufferedImage bufferedImage, int x, int y, Point st
}
}
int centerY = minY + (maxY - minY) / 2;
if (maxY - minY < 18) {
if (maxY - minY < ScreenAdapter.getMinWhiteHeight()) {
return null;
}
return new Point((int) (maxX * scaleX), (int) ((centerY)));
Expand Down
Loading

0 comments on commit b381e4d

Please sign in to comment.