Files
workphone-sdk/tools/quick_visible_capture_test.py

74 lines
2.3 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""快速验证脱线无线手机是否可亮屏并采集非黑屏截图。"""
from __future__ import annotations
import subprocess
import time
from datetime import datetime
from pathlib import Path
try:
from PIL import Image, ImageStat # type: ignore
except Exception:
Image = None
ImageStat = None
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / "开发文档" / "6、测试" / "visible_capture_fix_20260518" / datetime.now().strftime("%Y%m%d_%H%M%S")
OUT.mkdir(parents=True, exist_ok=True)
SERIAL = "192.168.0.12:5555"
PKG = "com.tencent.mm"
def adb(args: list[str], timeout: int = 10):
return subprocess.run(["adb", "-s", SERIAL] + args, cwd=str(ROOT), text=True, capture_output=True, timeout=timeout)
def phone_state() -> str:
p = adb(["shell", "dumpsys", "window"], timeout=8)
lines = []
for line in p.stdout.splitlines():
if "mCurrentFocus" in line or "mFocusedApp" in line or "mDreamingLockscreen" in line:
lines.append(line.strip())
return " | ".join(lines)
def ensure_visible():
adb(["shell", "svc", "power", "stayon", "true"])
adb(["shell", "input", "keyevent", "224"])
adb(["shell", "input", "keyevent", "82"])
time.sleep(0.3)
adb(["shell", "input", "swipe", "540", "2050", "540", "650", "500"])
time.sleep(0.5)
adb(["shell", "monkey", "-p", PKG, "-c", "android.intent.category.LAUNCHER", "1"], timeout=12)
time.sleep(1.5)
def is_black(path: Path) -> bool:
if Image is None or ImageStat is None:
return False
with Image.open(path) as img:
rgb = img.convert("RGB").resize((64, 128))
stat = ImageStat.Stat(rgb)
mean = sum(stat.mean) / 3.0
max_pixel = max(x[1] for x in rgb.getextrema())
return mean < 3 and max_pixel < 12
def capture(label: str) -> Path:
path = OUT / f"{label}.png"
with path.open("wb") as f:
subprocess.run(["adb", "-s", SERIAL, "exec-out", "screencap", "-p"], cwd=str(ROOT), stdout=f, stderr=subprocess.PIPE, timeout=20)
return path
def main():
ensure_visible()
path = capture("visible_after_wakeup")
print({"out": str(OUT), "shot": str(path), "black": is_black(path), "size": path.stat().st_size, "state": phone_state()})
if __name__ == "__main__":
main()