Skip to content
This repository has been archived by the owner on Mar 1, 2019. It is now read-only.

Commit

Permalink
fix connect remote device
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Mar 14, 2016
1 parent c61a134 commit 985b2f2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ d.stop_app(package_name)
里面的API是直接通过继承的方式支持的。

## FAQ
1. 如果连接远程机器上的安卓设备(测试过,并不好使,可能跟pyuiautomator依赖`adb forword`有关)
1. 如果连接远程机器上的安卓设备

远程机器上使用如下命令启动命令

Expand Down
47 changes: 26 additions & 21 deletions atx/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sys
import subprocess
import time
import tempfile
import threading
import warnings
import logging
Expand Down Expand Up @@ -164,6 +165,9 @@ def _run_watch(self):
sys.stdout.flush()
sys.stdout.write('\n')

def remove_force(name):
if os.path.isfile(name):
os.remove(name)

class CommonWrap(object):
def __init__(self):
Expand Down Expand Up @@ -298,10 +302,6 @@ def __init__(self, serialno=None, **kwargs):
# self._click_timeout = 20.0 # if icon not found in this time, then panic
# self._delay_after_click = 0.5 # when finished click, wait time
# self._loglock = threading.Lock()
# self._operation_mark = False

def _tmp_filename(self, prefix='tmp-', ext='.png'):
return '%s%s%s' %(prefix, time.time(), ext)

@property
def wlan_ip(self):
Expand Down Expand Up @@ -339,7 +339,7 @@ def _minicap_params(self):
"""
rotation = self.minicap_rotation
if self.minicap_rotation is None:
rotation = d.info['displayRotation']
rotation = self.info['displayRotation']

# rotation not working on SumSUNG 9502
return '{x}x{y}@{x}x{y}/{r}'.format(
Expand All @@ -348,31 +348,35 @@ def _minicap_params(self):
r=rotation*90)

def _screenshot_minicap(self):
phone_tmp_file = '/data/local/tmp/'+self._tmp_filename(ext='.jpg')
local_tmp_file = os.path.join(__tmp__, self._tmp_filename(ext='.jpg'))
phone_tmp_file = '/data/local/tmp/_atx_screen.jpg'
local_tmp_file = tempfile.mktemp(prefix='atx-tmp-', suffix='.jpg')
command = 'LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/minicap -P {} -s > {}'.format(
self._minicap_params(), phone_tmp_file)
self.adb_shell(command)
self.adb(['pull', phone_tmp_file, local_tmp_file])
self.adb_shell(['rm', phone_tmp_file])

try:
pil_image = Image.open(local_tmp_file)
self.adb_shell(command)
self.adb(['pull', phone_tmp_file, local_tmp_file])
image = imutils.open_as_pillow(local_tmp_file)

# Fix rotation not rotate right.
(img_w, img_h) = pil_image.size
if self.minicap_rotation in [1, 3] and img_w < img_h:
pil_image = pil_image.rotate(90, Image.BILINEAR, expand=True)
return pil_image
(width, height) = image.size
if self.minicap_rotation in [1, 3] and width < height:
image = image.rotate(90, Image.BILINEAR, expand=True)
return image
except IOError:
os.remove(local_tmp_file)
raise IOError("Screenshot use minicap failed.")
finally:
# remove_force(local_tmp_file)
self.adb_shell(['rm', phone_tmp_file])

def _screenshot_uiauto(self):
tmp_file = os.path.join(__tmp__, self._tmp_filename())
tmp_file = tempfile.mktemp(prefix='atx-tmp-', suffix='.jpg')
self._uiauto.screenshot(tmp_file)
screen = Image.open(tmp_file)
os.remove(tmp_file)
return screen
try:
return imutils.open_as_pillow(tmp_file)
except IOError:
raise IOError("Screenshot use uiautomator failed.")
finally:
remove_force(tmp_file)

def touch(self, x, y):
""" Alias for click """
Expand Down Expand Up @@ -446,6 +450,7 @@ def adb(self, command):
cmds.extend(list(command))
else:
cmds.append(command)
# print cmds
output = subprocess.check_output(cmds, stderr=subprocess.STDOUT)
return output.replace('\r\n', '\n')

Expand Down
18 changes: 12 additions & 6 deletions atx/imutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import numpy as np
from PIL import Image
from StringIO import StringIO

# import any special Python 2.7 packages
if sys.version_info.major == 2:
Expand All @@ -22,23 +23,28 @@
from urllib.request import urlopen


def read_image(self, img):
if isinstance(img, basestring):
return cv2.imread(img)
# FIXME(ssx): need support other types
return img
__sys_open = open


def open(image):
if isinstance(image, basestring):
name = image
if re.match(r'^https?://', name):
return url_to_image(name)
if os.path.isfile(name):
return cv2.imread(name)
img = cv2.imread(name)
if img is None:
raise IOError("Image format error: %s" % name)
raise IOError("Open image(%s) not found" % name)

return image

def open_as_pillow(filename):
""" This way can delete file immediately """
with __sys_open(filename, 'rb') as f:
data = StringIO(f.read())
return Image.open(data)


def from_pillow(pil_image):
""" Convert from pillow image to opencv """
Expand Down

0 comments on commit 985b2f2

Please sign in to comment.