fix: harden screenshot capture and diagnostics
This commit is contained in:
1 parent
ee3424cd9e
commit
f010853b97
19 files changed
+870
-70
No files matched your search
@@ -45,6 +45,26 @@ def _ops_log(msg: str, level: str = "INFO") -> None:
|
||||
pass
|
||||
|
||||
|
||||
def _adb_shell_png_variants(data: bytes) -> list[bytes]:
|
||||
if not data:
|
||||
return []
|
||||
variants: list[bytes] = []
|
||||
|
||||
def _add(candidate: bytes) -> None:
|
||||
if candidate and candidate not in variants:
|
||||
variants.append(candidate)
|
||||
|
||||
marker = b"\x89PNG"
|
||||
idx = data.find(marker)
|
||||
if idx > 0:
|
||||
data = data[idx:]
|
||||
|
||||
_add(data)
|
||||
_add(data.replace(b"\r\r\n", b"\r\n"))
|
||||
_add(data.replace(b"\r\n", b"\n"))
|
||||
return variants
|
||||
|
||||
|
||||
|
||||
|
||||
class ScreenshotOperations:
|
||||
@@ -169,6 +189,41 @@ class ScreenshotOperations:
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def _write_valid_png_bytes(data: bytes) -> bool:
|
||||
for candidate in _adb_shell_png_variants(data):
|
||||
try:
|
||||
with open(tmp_file, "wb") as f:
|
||||
f.write(candidate)
|
||||
if _valid_png(tmp_file):
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
def _try_shell_stdout_fallback() -> bool:
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[adb, "-s", device_id, "shell", "screencap", "-p"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
check=True,
|
||||
timeout=8,
|
||||
)
|
||||
if _write_valid_png_bytes(result.stdout or b"") and _safe_replace_file(tmp_file, out_file):
|
||||
if _valid_png(out_file):
|
||||
_mirror_screenshot_to_external(out_file)
|
||||
elapsed = (time.time() - direct_start) * 1000
|
||||
_ops_log(f"[{timetag}] [截图] [OK] 获取屏幕截图成功 (shell stdout回退,耗时: {elapsed:.0f}ms)")
|
||||
return True
|
||||
_ops_log(f"[截图] shell stdout回退输出无效: bytes={len(result.stdout or b'')}")
|
||||
except subprocess.CalledProcessError as e:
|
||||
_ops_log(f"[截图] shell stdout回退失败:ADB命令错误 (返回码: {e.returncode})")
|
||||
except subprocess.TimeoutExpired:
|
||||
_ops_log(f"[截图] shell stdout回退失败:操作超时 (设备ID: {device_id})")
|
||||
except Exception as e:
|
||||
_ops_log(f"[截图] shell stdout回退失败:{e}")
|
||||
return False
|
||||
|
||||
# 优先走 exec-out:只需 1 次 adb 调用;不在手机端落盘,减少 I/O 与卡顿
|
||||
# 为避免被并发读取到"半写入文件",写入 tmp 后再原子替换。
|
||||
for _ in range(2):
|
||||
@@ -314,7 +369,7 @@ class ScreenshotOperations:
|
||||
return False
|
||||
else:
|
||||
_ops_log("[截图] 回退方案截图无效")
|
||||
return False
|
||||
return _try_shell_stdout_fallback()
|
||||
except subprocess.CalledProcessError as e:
|
||||
# 特殊处理adb错误
|
||||
if e.returncode == 4294967295:
|
||||
@@ -325,6 +380,8 @@ class ScreenshotOperations:
|
||||
_ops_log(f"[截图] 回退方案失败:操作超时 (设备ID: {device_id})")
|
||||
except Exception as e:
|
||||
_ops_log(f"[截图] 回退方案失败:{e}")
|
||||
if _try_shell_stdout_fallback():
|
||||
return True
|
||||
return False
|
||||
|
||||
def _find_pic_path(self, picname: str, for_reading: bool = True) -> Optional[str]:
|
||||
|
||||
Reference in new issue
Block a user