调用adb
# !/user/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import subprocess
import cv2
import socket
import numpy as np
def clicked(x, y):
cmd = "adb shell input tap %s %s" % (x,y)
os.system(cmd)
def swip(x1,y1,x2,y2):
cmd = "adb shell input swipe %s %s %s %s" % (x1,y1,x2,y2)
os.system(cmd)
def back():
cmd = "adb shell input keyevent 4"
os.system(cmd)
def home():
cmd = "adb shell input keyevent 3"
os.system(cmd)
def closeApp():
cmd = "adb shell input keyevent 187"
os.system(cmd)
time.sleep(1)
clicked(543, 1708)
home()
time.sleep(1)
def CapScreen():
cmd = "adb shell screencap -p /sdcard/screen.png"
os.system(cmd)
cmd = "adb pull /sdcard/screen.png"
os.system(cmd)
# image = getScreenShot()
# cv2.imwrite('screen.png',image)
def getScreenShot():
# 使用subprocess的Popen调用adb shell命令,并将结果保存到PIPE管道�?
process = subprocess.Popen('adb shell screencap -p', shell=True, stdout=subprocess.PIPE)
# 读取管道中的数据
screenshot = process.stdout.read()
# 将读取的字节流数据的回车换行替换�?\n'
binary_screenshot = screenshot.replace(b'\r\n', b'\n')
# 使用numpy和imdecode将二进制数据转换成cv2的mat图片格式
img_screenshot = cv2.imdecode(np.frombuffer(binary_screenshot, np.uint8), cv2.IMREAD_COLOR)
return img_screenshot
def minicap():
screencap_adb = "adb shell \"LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/minicap -P 1080x1920@1080x1920/0 -s > /sdcard/screen.jpg\""
resp = os.popen(screencap_adb)
result=resp.read()
print(result)
cmd = "adb pull /sdcard/screen.jpg"
# print(cmd)
os.system(cmd)
def quickMiniCap(file):
image = getMinicapShot()
cv2.imwrite(file,image)
def getMinicapShot():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('localhost', 1717))
readBannerBytes = 0
bannerLength = 24
readFrameBytes = 0
frameBodyLengthRemaining = 0
frameBody = ''
while True:
chunk = client_socket.recv(12000)
if len(chunk) == 0:
continue
cursor = 0
while cursor < len(chunk):
# 跳过前面24位baner信息
if (readBannerBytes < bannerLength):
cursor += 1
readBannerBytes += 1
elif readFrameBytes < 4: # 在一开始的24位baner信息之后就是4位图片长度的信息
frameBodyLengthRemaining += (int(hex(chunk[cursor]), 16) << (readFrameBytes * 8))
cursor += 1
readFrameBytes += 1
else:
# 如果当前剩下的数据长度超过了图片还未读取的长度,说明这里包含了下一张图片的信息(由于不是连续读取,所以不考虑这些信息)
if len(chunk) - cursor >= frameBodyLengthRemaining:
frameBody = frameBody + chunk[cursor:(cursor + frameBodyLengthRemaining)]
if hex(frameBody[0]) != '0xff' or hex(frameBody[1]) != '0xd8':
exit()
img = np.array(bytearray(frameBody))
img = cv2.imdecode(img, 1)
client_socket.close()
return img
else:
# 这次数据的读取还未达到图片的数据的长�?
frameBody = bytes(list(frameBody) + list(chunk[cursor:len(chunk)]))
frameBodyLengthRemaining -= (len(chunk) - cursor)
readFrameBytes += len(chunk) - cursor
cursor = len(chunk)
if __name__ == '__main__':
closeApp()