改进android截图
parent
e558148d5a
commit
ea017d69e6
|
@ -0,0 +1,55 @@
|
||||||
|
package net.northking.cctp.upperComputer.driver.agent.command;
|
||||||
|
|
||||||
|
|
||||||
|
import net.northking.cctp.upperComputer.driver.agent.command.protocol.AbstractProtocolCommand;
|
||||||
|
import net.northking.cctp.upperComputer.driver.agent.command.protocol.ProtocolStreamUtil;
|
||||||
|
|
||||||
|
import java.io.DataInput;
|
||||||
|
import java.io.DataOutput;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class EchoCommand extends AbstractProtocolCommand<String> {
|
||||||
|
|
||||||
|
private static EchoCommand instance = null;
|
||||||
|
|
||||||
|
public static EchoCommand getInstance() {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = new EchoCommand();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String text = "";
|
||||||
|
|
||||||
|
private EchoCommand() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public EchoCommand(String text) {
|
||||||
|
if (text == null) throw new IllegalArgumentException("文本不能为null");
|
||||||
|
this.text = text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getText() {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setText(String text) {
|
||||||
|
if (text == null) throw new IllegalArgumentException("文本不能为null");
|
||||||
|
this.text = text;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCommandId() {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doSend(DataOutput output) throws IOException {
|
||||||
|
ProtocolStreamUtil.writeText(output, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String buildReceive(DataInput input) throws IOException {
|
||||||
|
return ProtocolStreamUtil.readText(input);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,82 @@
|
||||||
|
package net.northking.cctp.upperComputer.driver.agent.command.protocol;
|
||||||
|
|
||||||
|
import java.io.DataInput;
|
||||||
|
import java.io.DataOutput;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class ProtocolStreamUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送文本<br>
|
||||||
|
* 作为DataOutput方法的补充<br>
|
||||||
|
* 文本字节长度+文本字节
|
||||||
|
*
|
||||||
|
* @param output 发送操作对象
|
||||||
|
* @param text 要发送的文本内容
|
||||||
|
* @throws IOException 发送过程中发生输入输出错误
|
||||||
|
*/
|
||||||
|
public static void writeText(DataOutput output, String text) throws IOException {
|
||||||
|
byte[] textByteArray = text.getBytes();
|
||||||
|
output.writeInt(textByteArray.length);
|
||||||
|
output.write(textByteArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取文本<br>
|
||||||
|
* 作为DataInput方法的补充<br>
|
||||||
|
* 会读取一个整型作为接下来要读取的文本字节长度
|
||||||
|
*
|
||||||
|
* @param input 读取操作对象
|
||||||
|
* @return 读取到的文本
|
||||||
|
* @throws IOException 读取过程中发生输入输出错误
|
||||||
|
*/
|
||||||
|
public static String readText(DataInput input) throws IOException {
|
||||||
|
int textLength = input.readInt();
|
||||||
|
byte[] textByteArray = new byte[textLength];
|
||||||
|
input.readFully(textByteArray);
|
||||||
|
return new String(textByteArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[] readIntArray(DataInput input) throws IOException {
|
||||||
|
int length = input.readInt();
|
||||||
|
int[] array = new int[length];
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
array[i] = input.readInt();
|
||||||
|
}
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <K> void writeNullable(DataOutput output, K data, WriteNullableData<K> writer) throws IOException {
|
||||||
|
if (data == null) {
|
||||||
|
output.writeBoolean(false);
|
||||||
|
} else {
|
||||||
|
output.writeBoolean(true);
|
||||||
|
writer.write(output, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <K> K readNullable(DataInput input, ReadNullableData<K> reader) throws IOException {
|
||||||
|
if (input.readBoolean()) {
|
||||||
|
return reader.read(input);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ArrayList<String> readStringArrayList(DataInput input) throws IOException {
|
||||||
|
int length = input.readInt();
|
||||||
|
ArrayList<String> list = new ArrayList<String>(length);
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
list.add(readText(input));
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface ReadNullableData<K> {
|
||||||
|
K read(DataInput input) throws IOException;
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface WriteNullableData<K> {
|
||||||
|
void write(DataOutput output, K data) throws IOException;
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,10 +11,7 @@ import net.northking.cctp.upperComputer.driver.adb.Adb;
|
||||||
import net.northking.cctp.upperComputer.driver.adb.AdbDevice;
|
import net.northking.cctp.upperComputer.driver.adb.AdbDevice;
|
||||||
import net.northking.cctp.upperComputer.driver.adb.AdbTransport;
|
import net.northking.cctp.upperComputer.driver.adb.AdbTransport;
|
||||||
import net.northking.cctp.upperComputer.driver.agent.AndroidAgentSession;
|
import net.northking.cctp.upperComputer.driver.agent.AndroidAgentSession;
|
||||||
import net.northking.cctp.upperComputer.driver.agent.command.AppiumSourceXmlCommand;
|
import net.northking.cctp.upperComputer.driver.agent.command.*;
|
||||||
import net.northking.cctp.upperComputer.driver.agent.command.CloseSessionCommand;
|
|
||||||
import net.northking.cctp.upperComputer.driver.agent.command.GetSmsListCommand;
|
|
||||||
import net.northking.cctp.upperComputer.driver.agent.command.TakeScreenshotCommand;
|
|
||||||
import net.northking.cctp.upperComputer.enums.FileBusinessTypeEnum;
|
import net.northking.cctp.upperComputer.enums.FileBusinessTypeEnum;
|
||||||
import net.northking.cctp.upperComputer.entity.Attachment;
|
import net.northking.cctp.upperComputer.entity.Attachment;
|
||||||
import net.northking.cctp.upperComputer.entity.DebuggerDeviceInfo;
|
import net.northking.cctp.upperComputer.entity.DebuggerDeviceInfo;
|
||||||
|
@ -62,6 +59,8 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService {
|
||||||
|
|
||||||
private ConcurrentHashMap<String, AndroidDeviceAllInfoThread> queryDeviceInfoMap = new ConcurrentHashMap<>();
|
private ConcurrentHashMap<String, AndroidDeviceAllInfoThread> queryDeviceInfoMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private ConcurrentHashMap<String, AndroidAgentSession> screenShotAgentSessionMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private Adb adb;
|
private Adb adb;
|
||||||
|
|
||||||
@Value("${appium.server.host:127.0.0.1}")
|
@Value("${appium.server.host:127.0.0.1}")
|
||||||
|
@ -643,6 +642,10 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService {
|
||||||
@Override
|
@Override
|
||||||
public String shotAllScreen(DebuggerDeviceInfo info) {
|
public String shotAllScreen(DebuggerDeviceInfo info) {
|
||||||
File file = getScreenShotInCommand(info.getDeviceId());
|
File file = getScreenShotInCommand(info.getDeviceId());
|
||||||
|
if (null == file || !file.exists()) {
|
||||||
|
logger.error("设备【{}】截出来的图不存在",info.getDeviceId());
|
||||||
|
throw new ExecuteException("截取图片失败");
|
||||||
|
}
|
||||||
// File file = ScreenShotUtils.getMobileScreenShot(info.getDeviceId(), info.getResolution(), false);
|
// File file = ScreenShotUtils.getMobileScreenShot(info.getDeviceId(), info.getResolution(), false);
|
||||||
try {
|
try {
|
||||||
return Base64.encode(file);
|
return Base64.encode(file);
|
||||||
|
@ -663,6 +666,10 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService {
|
||||||
@Override
|
@Override
|
||||||
public String uploadShotAllScreen(DebuggerDeviceInfo info) {
|
public String uploadShotAllScreen(DebuggerDeviceInfo info) {
|
||||||
File file = getScreenShotInCommand(info.getDeviceId());
|
File file = getScreenShotInCommand(info.getDeviceId());
|
||||||
|
if (null == file || !file.exists()) {
|
||||||
|
logger.error("设备【{}】截出来的图不存在",info.getDeviceId());
|
||||||
|
throw new ExecuteException("截取图片失败");
|
||||||
|
}
|
||||||
String path = null;
|
String path = null;
|
||||||
try {
|
try {
|
||||||
//租户id
|
//租户id
|
||||||
|
@ -676,18 +683,18 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService {
|
||||||
path = upload.getUrlPath();
|
path = upload.getUrlPath();
|
||||||
}
|
}
|
||||||
} catch (ExecuteException e) {
|
} catch (ExecuteException e) {
|
||||||
logger.error("截图失败", e);
|
logger.error("上传截图失败", e);
|
||||||
throw e;
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("截图失败", e);
|
logger.error("上传截图失败", e);
|
||||||
throw new ExecuteException("截取图片失败");
|
throw new ExecuteException("上传截图失败");
|
||||||
} finally {
|
} finally {
|
||||||
if (file.exists()) {
|
// if (file.exists()) {
|
||||||
boolean delete = file.delete();
|
// boolean delete = file.delete();
|
||||||
if (!delete) {
|
// if (!delete) {
|
||||||
logger.warn("临时文件【{}】删除失败", file.getAbsolutePath());
|
// logger.warn("临时文件【{}】删除失败", file.getAbsolutePath());
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
@ -860,6 +867,10 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService {
|
||||||
Double realityX = info.getX().doubleValue() / screenWidth * realityWidth;
|
Double realityX = info.getX().doubleValue() / screenWidth * realityWidth;
|
||||||
Double realityY = info.getY().doubleValue() / screenHeight * realityHeight;
|
Double realityY = info.getY().doubleValue() / screenHeight * realityHeight;
|
||||||
snapShotFile = getScreenShotInCommand(info.getDeviceId());
|
snapShotFile = getScreenShotInCommand(info.getDeviceId());
|
||||||
|
if (null == snapShotFile || !snapShotFile.exists()) {
|
||||||
|
logger.error("设备【{}】截出来的图不存在",info.getDeviceId());
|
||||||
|
throw new ExecuteException("截取图片失败");
|
||||||
|
}
|
||||||
ImgUtil.cut(snapShotFile, cutFile, new java.awt.Rectangle(realityX.intValue(), realityY.intValue(), realityCutWidth.intValue(), realityCutHeight.intValue()));
|
ImgUtil.cut(snapShotFile, cutFile, new java.awt.Rectangle(realityX.intValue(), realityY.intValue(), realityCutWidth.intValue(), realityCutHeight.intValue()));
|
||||||
return cutFile;
|
return cutFile;
|
||||||
} catch (ExecuteException e) {
|
} catch (ExecuteException e) {
|
||||||
|
@ -879,13 +890,30 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService {
|
||||||
File tmpPicFile = null;
|
File tmpPicFile = null;
|
||||||
AndroidAgentSession agentSession = null;
|
AndroidAgentSession agentSession = null;
|
||||||
try {
|
try {
|
||||||
|
agentSession = screenShotAgentSessionMap.get(deviceId);
|
||||||
|
if (null == agentSession || !agentSession.isState()) {
|
||||||
AdbDevice currentDevice = AndroidDeviceManager.getInstance().getCurrentDevice(deviceId);
|
AdbDevice currentDevice = AndroidDeviceManager.getInstance().getCurrentDevice(deviceId);
|
||||||
if (null == currentDevice) {
|
if (null == currentDevice) {
|
||||||
logger.warn("设备【{}】不在线,请稍后再试",deviceId);
|
throw new ExecuteException("当前设备不在线");
|
||||||
throw new ExecuteException("当前设备已掉线无法截图");
|
|
||||||
}
|
}
|
||||||
agentSession = new AndroidAgentSession(currentDevice, false);
|
agentSession = new AndroidAgentSession(currentDevice, false);
|
||||||
agentSession.start();
|
agentSession.start();
|
||||||
|
screenShotAgentSessionMap.put(deviceId, agentSession);
|
||||||
|
} else {
|
||||||
|
String send = agentSession.send(new EchoCommand("hello")); //检查
|
||||||
|
if (!"hello".equals(send)) {
|
||||||
|
logger.warn("设备【{}】当前使用的截图的session失效了,重新创建一个。。。。。",deviceId);
|
||||||
|
AdbDevice currentDevice = AndroidDeviceManager.getInstance().getCurrentDevice(deviceId);
|
||||||
|
if (null == currentDevice) {
|
||||||
|
throw new ExecuteException("当前设备不在线");
|
||||||
|
}
|
||||||
|
agentSession = new AndroidAgentSession(currentDevice, false);
|
||||||
|
agentSession.start();
|
||||||
|
screenShotAgentSessionMap.put(deviceId, agentSession);
|
||||||
|
} else {
|
||||||
|
logger.debug("设备【{}】当前使用的截图的session还能用,继续使用。。。。。",deviceId);
|
||||||
|
}
|
||||||
|
}
|
||||||
byte[] screenShotData = agentSession.send(TakeScreenshotCommand.getInstance());
|
byte[] screenShotData = agentSession.send(TakeScreenshotCommand.getInstance());
|
||||||
if (null != screenShotData && screenShotData.length > 0) { //截图
|
if (null != screenShotData && screenShotData.length > 0) { //截图
|
||||||
logger.debug("收到手机【{}】截图,大小:{}", deviceId, screenShotData.length);
|
logger.debug("收到手机【{}】截图,大小:{}", deviceId, screenShotData.length);
|
||||||
|
@ -904,9 +932,7 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("设备【"+deviceId+"】截图异常", e);
|
logger.error("设备【"+deviceId+"】截图异常", e);
|
||||||
}finally {
|
}finally {
|
||||||
if (null != agentSession) {
|
logger.debug("设备【{}】写到本地的图片大小:{}");
|
||||||
agentSession.closeSilence();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return tmpPicFile;
|
return tmpPicFile;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue