-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from bambootang/update-0.0.5
Update 0.0.5
- Loading branch information
Showing
10 changed files
with
244 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 105 additions & 52 deletions
157
src/main/java/com/company/playJumpJumpWithMouse/AdbCaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.