Просмотр исходного кода

feat(device): can screenshot in mumu,ld and nox

runhey 3 лет назад
Родитель
Сommit
1436f207b6
2 измененных файлов с 144 добавлено и 2 удалено
  1. 80 2
      module/device/handle.py
  2. 64 0
      module/device/method/window.py

+ 80 - 2
module/device/handle.py

@@ -4,13 +4,17 @@
 import re
 import re
 
 
 from cached_property import cached_property
 from cached_property import cached_property
-from anytree import NodeMixin, RenderTree
+from anytree import NodeMixin, RenderTree, PreOrderIter
+from win32api import GetSystemMetrics, SendMessage, MAKELONG, PostMessage
+from win32print import GetDeviceCaps
 from win32process import GetWindowThreadProcessId
 from win32process import GetWindowThreadProcessId
 from win32gui import (GetWindowText, EnumWindows, FindWindow, FindWindowEx,
 from win32gui import (GetWindowText, EnumWindows, FindWindow, FindWindowEx,
                       IsWindow, GetWindowRect, GetWindowDC, DeleteObject,
                       IsWindow, GetWindowRect, GetWindowDC, DeleteObject,
                       SetForegroundWindow, IsWindowVisible, GetDC, GetParent,
                       SetForegroundWindow, IsWindowVisible, GetDC, GetParent,
                       EnumChildWindows)
                       EnumChildWindows)
-
+from win32con import (SRCCOPY, DESKTOPHORZRES, DESKTOPVERTRES, WM_LBUTTONUP,
+                      WM_LBUTTONDOWN, WM_ACTIVATE, WA_ACTIVE, MK_LBUTTON,
+                      WM_NCHITTEST, WM_SETCURSOR, HTCLIENT, WM_MOUSEMOVE)
 from module.config.config import Config
 from module.config.config import Config
 from module.exception import RequestHumanTakeover, EmulatorNotRunningError
 from module.exception import RequestHumanTakeover, EmulatorNotRunningError
 from module.logger import logger
 from module.logger import logger
@@ -50,6 +54,20 @@ def handle_num2pid(num :int) -> int:
     """
     """
     return 0 if num is None or num == 0 or num == '' else GetWindowThreadProcessId(num)[1]
     return 0 if num is None or num == 0 or num == '' else GetWindowThreadProcessId(num)[1]
 
 
+def window_scale_rate() -> float:
+    """
+    获取window的系统缩放 一遍是1
+    :return:
+    """
+    hDC = GetDC(0)
+    # 物理上(真实的)的 横纵向分辨率
+    wReal = GetDeviceCaps(hDC, DESKTOPHORZRES)
+    hReal = GetDeviceCaps(hDC, DESKTOPVERTRES)
+    # 缩放后的 分辨率
+    wAfter = GetSystemMetrics(0)
+    hAfter = GetSystemMetrics(1)
+    # print(wReal, wAfter)
+    return round(wReal / wAfter, 2)
 
 
 class WindowNode(NodeMixin):
 class WindowNode(NodeMixin):
     def __init__(self, name, num, parent=None):
     def __init__(self, name, num, parent=None):
@@ -127,6 +145,18 @@ class Handle:
         logger.info('the emulator handle structure is')
         logger.info('the emulator handle structure is')
         for pre, fill, node in RenderTree(self.root_node):
         for pre, fill, node in RenderTree(self.root_node):
             logger.info("%s%s" % (pre, node.name))
             logger.info("%s%s" % (pre, node.name))
+        for pre, fill, node in RenderTree(self.root_node):
+            logger.info("%s%s" % (pre, node.num))
+
+        # 判断是哪一个模拟器
+        logger.info(f'the emulator family is {self.emulator_family}')
+
+        # window系统的缩放
+        logger.info(f'your window screen scale rate is {window_scale_rate()}')
+        _ = self.screenshot_handle_num
+        logger.info(f'screenshot handle num is {self.screenshot_handle_num}')
+        logger.info(f'the emulator screenshot size is {self.screenshot_size}')
+
 
 
     @staticmethod
     @staticmethod
     def all_windows() -> list:
     def all_windows() -> list:
@@ -203,6 +233,54 @@ class Handle:
                 elif emu == '逍遥':
                 elif emu == '逍遥':
                     return 'memu_player_family'
                     return 'memu_player_family'
 
 
+    @cached_property
+    def screenshot_handle_num(self) -> int:
+        """
+        截屏的句柄其实并不是根句柄
+        :return:  出错返回None
+        """
+        if self.emulator_family == 'mumu_player_family':
+            if re.match(string=self.root_handle_title, pattern=r".*12$"):
+                logger.info('is mumu12')
+                for node in PreOrderIter(self.root_node):
+                    if node.name == Handle.emulator_handle['mumu_player_12'][1]:
+                        return node.num
+            else:
+                logger.info('is mumu')
+                for node in PreOrderIter(self.root_node):
+                    if node.name == Handle.emulator_handle['mumu_player'][1]:
+                        return node.num
+        # 夜神
+        elif self.emulator_family == 'nox_player_family':
+            return self.root_node.children[1].children[1].num
+
+        elif self.emulator_family == 'ld_player_family':
+            for node in PreOrderIter(self.root_node):
+                if node.name == Handle.emulator_handle['ld_player_family'][0]:
+                    return node.num
+
+        elif self.emulator_family == 'memu_player_family':
+            for node in PreOrderIter(self.root_node):
+                if node.name == Handle.emulator_handle['memu_player_family']:
+                    return node.num
+
+        elif self.emulator_family == 'bluestacks_family':
+            for node in PreOrderIter(self.root_node):
+                if node.name == Handle.emulator_handle['bluestacks_family']:
+                    return node.num
+        return None
+
+    @cached_property
+    def screenshot_size(self) -> tuple:
+        """
+        第一个是width 第二个是heigth
+        :return:
+        """
+        winRect = GetWindowRect(self.screenshot_handle_num)
+        width: int = winRect[2] - winRect[0]  # 右x-左x
+        height: int = winRect[3] - winRect[1]  # 下y - 上y 计算高度
+        return width, height
+
 if __name__ == '__main__':
 if __name__ == '__main__':
     h = Handle(config='oas1')
     h = Handle(config='oas1')
     logger.info(h.auto_handle_title(h.all_windows()))
     logger.info(h.auto_handle_title(h.all_windows()))

+ 64 - 0
module/device/method/window.py

@@ -1,3 +1,67 @@
 # This Python file uses the following encoding: utf-8
 # This Python file uses the following encoding: utf-8
 # @author runhey
 # @author runhey
 # github https://github.com/runhey
 # github https://github.com/runhey
+import cv2
+from numpy import frombuffer, uint8, array, random
+import numpy as np
+import time
+from win32gui import (GetWindowText, EnumWindows, FindWindow, FindWindowEx,
+                      IsWindow, GetWindowRect, GetWindowDC, DeleteObject,
+                      SetForegroundWindow, IsWindowVisible, GetDC, GetParent,
+                      EnumChildWindows)
+from win32ui import CreateDCFromHandle, CreateBitmap
+from win32con import SRCCOPY
+
+from module.exception import RequestHumanTakeover, ScriptError
+from module.logger import logger
+from module.device.handle import Handle, window_scale_rate
+
+class Window(Handle):
+
+    def screenshot_window_background(self):
+        """
+        后台截屏
+        :return:
+        """
+        win_size = self.screenshot_size
+        scale_rate = window_scale_rate()
+        widthScreen = int(win_size[0] * scale_rate)
+        heightScreen = int(win_size[1] * scale_rate)
+        # 返回句柄窗口的设备环境,覆盖整个窗口,包括非客户区,标题栏,菜单,边框
+        hwndDc = GetWindowDC(self.screenshot_handle_num)
+        # 创建设备描述表
+        mfcDc = CreateDCFromHandle(hwndDc)
+        # 创建内存设备描述表
+        saveDc = mfcDc.CreateCompatibleDC()
+        # 创建位图对象准备保存图片
+        saveBitMap = CreateBitmap()
+        # 为bitmap开辟存储空间
+        saveBitMap.CreateCompatibleBitmap(mfcDc, widthScreen, heightScreen)
+        # 将截图保存到saveBitMap中
+        saveDc.SelectObject(saveBitMap)
+        # 保存bitmap到内存设备描述表
+        saveDc.BitBlt((0, 0), (widthScreen, heightScreen), mfcDc, (0, 0), SRCCOPY)
+
+        # 保存图像
+        signedIntsArray = saveBitMap.GetBitmapBits(True)
+        imgSrceen = frombuffer(signedIntsArray, dtype='uint8')
+        imgSrceen.shape = (heightScreen, widthScreen, 4)
+        # imgSrceen = cv2.cvtColor(imgSrceen, cv2.COLOR_BGRA2GRAY)
+        imgSrceen = cv2.resize(imgSrceen, (win_size[0], win_size[1]))
+
+        # 测试显示截图图片
+        cv2.namedWindow('imgSrceen')  # 命名窗口
+        cv2.imshow("imgSrceen", imgSrceen)  # 显示
+        cv2.waitKey(0)
+        cv2.destroyAllWindows()
+
+        # 内存释放
+        DeleteObject(saveBitMap.GetHandle())
+        saveDc.DeleteDC()
+        mfcDc.DeleteDC()
+        return imgSrceen
+
+
+if __name__ == "__main__":
+    w = Window(config='oas1')
+    w.screenshot_window_background()