改进android截图

master
yineng.huang 2024-07-09 11:50:13 +08:00
parent e558148d5a
commit ea017d69e6
3 changed files with 185 additions and 22 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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.AdbTransport;
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.CloseSessionCommand;
import net.northking.cctp.upperComputer.driver.agent.command.GetSmsListCommand;
import net.northking.cctp.upperComputer.driver.agent.command.TakeScreenshotCommand;
import net.northking.cctp.upperComputer.driver.agent.command.*;
import net.northking.cctp.upperComputer.enums.FileBusinessTypeEnum;
import net.northking.cctp.upperComputer.entity.Attachment;
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, AndroidAgentSession> screenShotAgentSessionMap = new ConcurrentHashMap<>();
private Adb adb;
@Value("${appium.server.host:127.0.0.1}")
@ -643,6 +642,10 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService {
@Override
public String shotAllScreen(DebuggerDeviceInfo info) {
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);
try {
return Base64.encode(file);
@ -663,6 +666,10 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService {
@Override
public String uploadShotAllScreen(DebuggerDeviceInfo info) {
File file = getScreenShotInCommand(info.getDeviceId());
if (null == file || !file.exists()) {
logger.error("设备【{}】截出来的图不存在",info.getDeviceId());
throw new ExecuteException("截取图片失败");
}
String path = null;
try {
//租户id
@ -676,18 +683,18 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService {
path = upload.getUrlPath();
}
} catch (ExecuteException e) {
logger.error("截图失败", e);
logger.error("上传截图失败", e);
throw e;
} catch (Exception e) {
logger.error("截图失败", e);
throw new ExecuteException("失败");
logger.error("上传截图失败", e);
throw new ExecuteException("上传截图失败");
} finally {
if (file.exists()) {
boolean delete = file.delete();
if (!delete) {
logger.warn("临时文件【{}】删除失败", file.getAbsolutePath());
}
}
// if (file.exists()) {
// boolean delete = file.delete();
// if (!delete) {
// logger.warn("临时文件【{}】删除失败", file.getAbsolutePath());
// }
// }
}
return path;
}
@ -860,6 +867,10 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService {
Double realityX = info.getX().doubleValue() / screenWidth * realityWidth;
Double realityY = info.getY().doubleValue() / screenHeight * realityHeight;
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()));
return cutFile;
} catch (ExecuteException e) {
@ -879,13 +890,30 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService {
File tmpPicFile = null;
AndroidAgentSession agentSession = null;
try {
AdbDevice currentDevice = AndroidDeviceManager.getInstance().getCurrentDevice(deviceId);
if (null == currentDevice) {
logger.warn("设备【{}】不在线,请稍后再试",deviceId);
throw new ExecuteException("当前设备已掉线无法截图");
agentSession = screenShotAgentSessionMap.get(deviceId);
if (null == agentSession || !agentSession.isState()) {
AdbDevice currentDevice = AndroidDeviceManager.getInstance().getCurrentDevice(deviceId);
if (null == currentDevice) {
throw new ExecuteException("当前设备不在线");
}
agentSession = new AndroidAgentSession(currentDevice, false);
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);
}
}
agentSession = new AndroidAgentSession(currentDevice,false);
agentSession.start();
byte[] screenShotData = agentSession.send(TakeScreenshotCommand.getInstance());
if (null != screenShotData && screenShotData.length > 0) { //截图
logger.debug("收到手机【{}】截图,大小:{}", deviceId, screenShotData.length);
@ -904,9 +932,7 @@ public class AndroidDebuggerServiceImpl extends AbstractDebuggerService {
} catch (Exception e) {
logger.error("设备【"+deviceId+"】截图异常", e);
}finally {
if (null != agentSession) {
agentSession.closeSilence();
}
logger.debug("设备【{}】写到本地的图片大小:{}");
}
return tmpPicFile;
}