uiautomator 横向截屏异常解决
作者:互联网
这里写自定义目录标题
uiautomator 横向截屏异常解决
在使用uiautomator2 同时控制多台手机时,发现部分手机截图有问题。
横屏截图依旧是竖过来的。但群控工具上实时画面是正常的。
正常情况下,向ATX-agent 发送 rotation请求,minicap就会重启进行旋转截图。
看 uiautomator2 源码发现截图的原理是 向手机上的ATX-agent发送请求,拿到图片。
def screenshot_uri(self):
print(self._host, self._port)
#http://10.12.200.48:7912/screenshot/0?minicap=true
return 'http://%s:%d/screenshot/0' % (self._host, self._port)
之后又看了ATX-agent的源码
// android emulator use screencap
// then minicap when binary and .so exists
// then uiautomator when service(uiautomator) is running
// last screencap
method := "screencap"
if getCachedProperty("ro.product.cpu.abi") == "x86" { // android emulator
method = "screencap"
} else if fileExists("/data/local/tmp/minicap") && fileExists("/data/local/tmp/minicap.so") && r.FormValue("minicap") != "false" && strings.ToLower(getCachedProperty("ro.product.manufacturer")) != "meizu" {
method = "minicap"
} else if service.Running("uiautomator") {
method = "uiautomator"
}
我猜大致的意思就是uiautomator2的截图接口是其他的。并不是minicap的图片,
因为在abdshell 看进程 minicap进程是正常的,带有旋转角度90的(写文章的时候才意识到,是不是minicap的宽高是错的,忘记看了)。
所以重写了uiautomator2 一个新的截图方法(按照里面xpath.py的代码写的),代码如下
def minicapscreenshot(self):
try:
import adbutils
from PIL import Image
d = adbutils.adb.device(serial=self.server.serial )
json_output = d.shell(
["LD_LIBRARY_PATH=/data/local/tmp", "/data/local/tmp/minicap", "-i", "2&>/dev/null"]).strip()
data = json.loads(json_output)
# 宽高互换
w, h, r = data["height"],data["width"], data["rotation"]
r = 90
remote_image_path = "/sdcard/minicap.jpg"
d.shell(["rm", remote_image_path])
d.shell([
"LD_LIBRARY_PATH=/data/local/tmp",
"/data/local/tmp/minicap",
"-P", "{0}x{1}@{0}x{1}/{2}".format(w, h, r),
"-s", ">" + remote_image_path])
if d.sync.stat(remote_image_path).size == 0:
raise RuntimeError("screenshot using minicap error")
buf = io.BytesIO()
for data in d.sync.iter_content(remote_image_path):
buf.write(data)
# pillow形式的图片
return Image.open(buf)
except Exception as e:
print(Exception,e)
现在遇到的问题就是怎么把pillow的图片转成cv2的灰度图。。。我直接转的,结果生成的是蓝色的图片。最后没办法就是先保存pillow图片,再用cv2打开的。
现在想做 手机游戏里按钮的点击,但是手机分辨率不一样,背景不一样,按钮的匹配效果不是很好。求各位大神告知一下哪有相关的一些资料。
标签:tmp,uiautomator,local,self,横向,截屏,minicap,data 来源: https://blog.csdn.net/qq_18946341/article/details/120198406