代码拉取完成,页面将自动刷新
同步操作将从 调试中.../Minicontrol 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# -*- coding: utf-8 -*-
# !/usr/bin/env python
"""
Created on Tue Dec 20 10:06:11 2016
@author: yao.fu
"""
import os
import thread
import threading
import time
import cv2
from PyQt4 import QtGui
from PyQt4.QtCore import Qt, QDir, pyqtSignal
from PyQt4.QtGui import QDialog, QPixmap
from AdbUtils import adb_send_key_event, adb_send_text, is_adb_set, set_serial, \
adb_get_devices
from AndroidKeyCode import KEYCODE_POWER, KEYCODE_HOME, KEYCODE_MENU, KEYCODE_APP_SWITCH, KEYCODE_BACK, \
KEYCODE_VOLUME_UP, KEYCODE_VOLUME_DOWN
from CommonUtils import cover_touch_to_cmd, get_common_cmd, STATE_DISCONNECT, STATE_CONNECTING, STATE_CONNECTED, \
DEFINE_HELP_MESSAGE
from Config import DEF_CONFIG_CAPTURE_PATH, DEF_MAX_RETRY_TIMES
from DebugHelper import log_debug, LOG_TAG_MAIN_WINDOW
from DialogChooseDevice import DialogChooseDevice
from LabelCustom import LabelCustom
from Main_window import Ui_MainWindow, _fromUtf8
from MiniCapture import MiniCapture
from MiniTouch import MiniTouch
from SettingDialog import SettingDialog
class MiniControlWindow(QtGui.QMainWindow):
# mode; state; result; retry time
notify = pyqtSignal(int, int, int, int, int)
def __init__(self, parent=None):
super(MiniControlWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.label = LabelCustom()
pixmap = QPixmap()
pixmap.load(QDir().currentPath() + "/resource/logo.jpg")
self.label.setPixmap(pixmap)
# self.label.setFixedSize(pixmap.width(), pixmap.height())
self.label.action.connect(self.on_touch_event, Qt.QueuedConnection)
self.ui.verticalLayout.insertWidget(0, self.label)
self.capture = STATE_DISCONNECT
self.touch = STATE_DISCONNECT
self.threadLock = threading.Lock()
self.notify.connect(self.on_init_ret, Qt.QueuedConnection)
self.device_serial = ""
self.device_info = "No device"
self.mini_cap = None
self.mini_touch = None
if is_adb_set() < 0:
self.open_setting_dialog()
else:
self.check_device_and_action()
def on_init_ret(self, mode, cap_state, touch_state, ret, times):
self.showMsg(cap_state, touch_state, ret, times)
# if ret == 0:
# if mode == 0:
# self.update_cap_timer.start()
# elif mode == 1:
# pass
# else:
# if mode == 0:
# self.update_cap_timer.stop()
# is_start value: 0 stop; 1 start
def startStopMini(self, is_start=1):
if is_start == 1:
thread.start_new_thread(self.miniWork, ("start miniThread", is_start))
else:
thread.start_new_thread(self.miniWork, ("stop miniThread", is_start))
def miniWork(self, name, is_start):
self.threadLock.acquire()
adb = is_adb_set()
log_debug(LOG_TAG_MAIN_WINDOW, "Start Mini Work is_start %s adb is %s" % (is_start, adb))
if adb < 0:
is_start = 0
if is_start == 1:
if self.capture is STATE_DISCONNECT:
self.capture = STATE_CONNECTING
ret = -1
retry_time = 0
while ret != 0 and retry_time < DEF_MAX_RETRY_TIMES:
retry_time = retry_time + 1
self.notify.emit(0, self.capture, self.touch, 1, retry_time)
ret = self.init_mini_cap()
if ret < 0:
self.capture = STATE_DISCONNECT
else:
self.capture = STATE_CONNECTED
self.notify.emit(0, self.capture, self.touch, ret, 0)
if self.touch is STATE_DISCONNECT:
self.touch = STATE_CONNECTING
ret = -1
retry_time = 0
while ret != 0 and retry_time < DEF_MAX_RETRY_TIMES:
retry_time = retry_time + 1
self.notify.emit(1, self.capture, self.touch, 1, retry_time)
ret = self.init_mini_touch()
if ret != 0:
self.touch = STATE_DISCONNECT
else:
self.touch = STATE_CONNECTED
self.notify.emit(1, self.capture, self.touch, ret, 0)
elif is_start == 0:
if self.capture is not STATE_DISCONNECT:
self.capture = STATE_DISCONNECT
if self.mini_cap is not None:
self.mini_cap.stop_mini_cap()
self.mini_cap = None
self.notify.emit(0, self.capture, self.touch, 1, 0)
if self.touch is not STATE_DISCONNECT:
self.touch = STATE_DISCONNECT
if self.mini_touch is not None:
self.mini_touch.stop_mini_touch()
self.mini_touch = None
self.notify.emit(1, self.capture, self.touch, 1, 0)
if adb < 0:
self.notify.emit(-1, self.capture, self.touch, adb, 0)
self.threadLock.release()
def showMsg(self, cap_state, touch_state, ret, times):
if ret < 0:
if ret == -1:
msg = "ERROR: device not connect!"
else:
msg = "ERROR: adb path not set!"
else:
msg = ""
if cap_state == STATE_CONNECTED:
msg = msg + "connected capture(port:" + str(self.mini_cap.port) + ");"
elif cap_state == STATE_CONNECTING:
msg = msg + "connecting capture(try times:" + str(times) + ");"
elif cap_state == STATE_DISCONNECT:
msg = msg + "disconnected capture;"
if touch_state == STATE_CONNECTED:
msg = msg + "connected touch(port:" + str(self.mini_touch.port) + ");"
elif touch_state == STATE_CONNECTING:
msg = msg + "connecting touch(try times:" + str(times) + ");"
if touch_state == STATE_DISCONNECT:
msg = msg + "disconnected touch;"
msg = msg + "(" + self.device_info + ")"
self.ui.statusbar.showMessage(msg)
def init_mini_cap(self):
if self.mini_cap is None:
self.mini_cap = MiniCapture()
self.mini_cap.get_signal_frame().connect(self.update_capture, Qt.QueuedConnection)
ret = self.mini_cap.prepare()
if ret < 0:
return ret
ret = self.mini_cap.start_mini_cap()
if ret < 0:
self.mini_cap.stop_mini_cap()
return ret
return 0
def check_device_and_action(self):
devices, has = adb_get_devices()
if len(devices) > 1:
self.select_device()
elif len(devices) == 1:
self.device_serial = devices[0]["serial"]
self.device_info = devices[0]["info"]
set_serial(self.device_serial)
self.startStopMini()
else:
self.device_info = "No device"
self.startStopMini(0)
def open_setting_dialog(self):
dialog = SettingDialog(self)
if dialog.exec_() == QDialog.Accepted:
if dialog.is_change:
self.startStopMini(0)
self.check_device_and_action()
def init_mini_touch(self):
if self.mini_touch is None:
self.mini_touch = MiniTouch()
ret = self.mini_touch.prepare()
if ret == 0:
ret = self.mini_touch.start_mini_touch()
else:
self.mini_touch.stop_mini_touch()
return ret
def send_touch_info(self):
if self.mini_touch is None:
return
self.mini_touch.send_to_server()
def update_capture(self, frame):
if frame is None or frame.any() is False:
return
# The width of the image width must alignment with 4 times
__width = frame.shape[1] / 4
if __width > 0:
__image_resize = frame[:, 0:__width * 4]
if __image_resize is not None:
__image_resize = cv2.cvtColor(__image_resize, cv2.COLOR_BGR2RGB)
capture = QtGui.QImage(__image_resize.data, __image_resize.shape[1],
__image_resize.shape[0], QtGui.QImage.Format_RGB888)
self.label.resize(capture.width(), capture.height())
self.label.setPixmap(QtGui.QPixmap.fromImage(capture))
self.label.show()
def get_capture_frame(self):
if self.mini_cap is not None:
ret, frame = self.mini_cap.get_capture()
if ret is False:
return None
# The width of the image width must alignment with 4 times
__width = frame.shape[1] / 4
if __width > 0:
__image_resize = frame[:, 0:__width * 4]
return __image_resize
def on_touch_event(self, action, event):
if self.touch == STATE_CONNECTED:
rect = self.label.frameRect()
dis_w = rect.width()
dis_h = rect.height()
real_w = self.mini_cap.mini_cap_info.banner_real_width
real_h = self.mini_cap.mini_cap_info.banner_real_height
x = (event.x() * real_w) / dis_w
y = (event.y() * real_h) / dis_h
cmd = cover_touch_to_cmd(action, x, y)
ret = self.mini_touch.send_to_server(cmd)
if ret < 0:
self.ui.statusbar.showMessage("Mini touch connect error, Please reconnect!")
cmd = get_common_cmd()
ret = self.mini_touch.send_to_server(cmd)
if ret < 0:
self.ui.statusbar.showMessage("Mini touch connect error, Please reconnect!")
def closeEvent(self, *args, **kwargs):
# self.update_cap_timer.stop()
if self.mini_cap is not None:
self.mini_cap.stop_mini_cap()
if self.mini_touch is not None:
self.mini_touch.stop_mini_touch()
super(MiniControlWindow, self).closeEvent(*args, **kwargs)
def onActionTriggered(self, action):
if action == self.ui.actionSetting:
self.open_setting_dialog()
elif action == self.ui.actionConnect:
self.startStopMini()
elif action == self.ui.actionDisconnect:
self.startStopMini(0)
elif action == self.ui.actionHelp:
QtGui.QMessageBox.information(self, "About", _fromUtf8(DEFINE_HELP_MESSAGE))
elif action == self.ui.actionChoose:
self.select_device()
def select_device(self):
dialog = DialogChooseDevice(self, self.device_serial)
if dialog.exec_() == QDialog.Accepted:
select_item = dialog.get_select_item()
if select_item:
serial = select_item["serial"]
if self.device_serial != serial:
self.device_serial = serial
self.device_info = select_item["info"]
set_serial(self.device_serial)
self.startStopMini(0)
self.startStopMini(1)
else:
self.device_serial = ""
self.device_info = "No device"
set_serial(self.device_serial)
self.startStopMini(0)
def onPowerPress(self):
pass
def onPowerRelease(self):
adb_send_key_event(KEYCODE_POWER)
def onHomePress(self):
pass
def onHomeRelease(self):
adb_send_key_event(KEYCODE_HOME)
def onMenuPress(self):
pass
def onMenuRelease(self):
adb_send_key_event(KEYCODE_MENU)
def onRecentsPress(self):
pass
def onRecentsRelease(self):
adb_send_key_event(KEYCODE_APP_SWITCH)
def onBackPress(self):
pass
def onBackRelease(self):
adb_send_key_event(KEYCODE_BACK)
def onVolIncPress(self):
pass
def onVolIncRelease(self):
adb_send_key_event(KEYCODE_VOLUME_UP)
def onVolDecPress(self):
pass
def onVolDecRelease(self):
adb_send_key_event(KEYCODE_VOLUME_DOWN)
def onCapturePress(self):
pass
def onCaptureRelease(self):
frame = self.get_capture_frame()
if frame is not None:
name = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime(time.time()))
path = DEF_CONFIG_CAPTURE_PATH
if os.path.exists(path) is False:
os.makedirs(path)
path = str(path + name + ".png")
ret = cv2.imwrite(path, frame)
print("save img to ", path, ret)
else:
print("Cannot get frame")
def onSendClick(self):
text = self.ui.lineEdit.text().toUtf8()
if text is not None and len(text) > 0:
adb_send_text(text)
if __name__ == '__main__':
import sys
log_debug(LOG_TAG_MAIN_WINDOW, "enter app")
app = QtGui.QApplication(sys.argv)
window = MiniControlWindow()
window.setWindowTitle('MiniControl')
window.show()
log_debug(LOG_TAG_MAIN_WINDOW, "exit app")
sys.exit(app.exec_())
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。