diff --git a/cpp/winsharedutils/screenshot.cpp b/cpp/winsharedutils/screenshot.cpp index cda53b2f87e..c18dcd5bc2f 100644 --- a/cpp/winsharedutils/screenshot.cpp +++ b/cpp/winsharedutils/screenshot.cpp @@ -184,8 +184,9 @@ std::vector __gdi_screenshot(HWND hwnd, RECT rect) ReleaseDC(hwnd, hdc); return std::move(bf); } -DECLARE_API void gdi_screenshot(HWND hwnd, RECT rect, void (*cb)(byte *, size_t)) +DECLARE_API void gdi_screenshot(HWND hwnd, void (*cb)(byte *, size_t)) { + RECT rect{-1, -1, -1, -1}; auto bf = __gdi_screenshot(hwnd, rect); if (bf.size()) cb(bf.data(), bf.size()); diff --git a/py/LunaTranslator/myutils/hwnd.py b/py/LunaTranslator/myutils/hwnd.py index 71a41f923e5..5dbc4ea8153 100644 --- a/py/LunaTranslator/myutils/hwnd.py +++ b/py/LunaTranslator/myutils/hwnd.py @@ -73,14 +73,14 @@ def callback_1(callback_origin, uid, tocliponly, p: QPixmap, fn): if not hwnd: return hwnd = windows.GetAncestor(hwnd) - p = gdi_screenshot(-1, -1, -1, -1, hwnd) + p = safepixmap(winsharedutils.gdi_screenshot(hwnd)) callback(p, fname + "_gdi." + app) isshit = (not callback_origin) and (not tocliponly) if p.isNull() or isshit: @threader def _(): - p = winrt_capture_window(hwnd) + p = safepixmap(winrtutils.winrt_capture_window(hwnd)) callback(p, fname + "_winrt." + app) _() @@ -95,7 +95,7 @@ def _(): @threader def _(): - p = winrt_capture_window(hwnd) + p = safepixmap(winrtutils.winrt_capture_window(hwnd)) callback(p, fname + "_winrt_magpie." + app) _() @@ -263,21 +263,6 @@ def safepixmap(bs): return pixmap -def crop_image(x1, y1, x2, y2, hwnd=None): - bs = winsharedutils.crop_image(x1, y1, x2, y2, hwnd) - return safepixmap(bs) - - -def gdi_screenshot(x1, y1, x2, y2, hwnd=None): - bs = winsharedutils.gdi_screenshot(x1, y1, x2, y2, hwnd) - return safepixmap(bs) - - -def winrt_capture_window(hwnd): - bs = winrtutils.winrt_capture_window(hwnd) - return safepixmap(bs) - - def subprochiderun(cmd, cwd=None, encoding="utf8") -> subprocess.CompletedProcess: startupinfo = subprocess.STARTUPINFO() diff --git a/py/LunaTranslator/myutils/ocrutil.py b/py/LunaTranslator/myutils/ocrutil.py index 7d6fb9dd7c1..e876db1a2ed 100644 --- a/py/LunaTranslator/myutils/ocrutil.py +++ b/py/LunaTranslator/myutils/ocrutil.py @@ -2,14 +2,14 @@ from myutils.config import globalconfig, _TR from qtsymbols import * from myutils.commonbase import ArgsEmptyExc -from myutils.hwnd import crop_image +from myutils.hwnd import safepixmap from myutils.utils import stringfyerror, qimage2binary from traceback import print_exc -import threading, gobject +import threading, gobject, winsharedutils def imageCut(hwnd, x1, y1, x2, y2) -> QImage: - pix = crop_image(x1, y1, x2, y2, hwnd) + pix = safepixmap(winsharedutils.crop_image(x1, y1, x2, y2, hwnd)) image = pix.toImage() gobject.baseobject.maybesetimage(image) return image diff --git a/py/LunaTranslator/winsharedutils.py b/py/LunaTranslator/winsharedutils.py index 41f1d326d3a..8ce30d55bd6 100644 --- a/py/LunaTranslator/winsharedutils.py +++ b/py/LunaTranslator/winsharedutils.py @@ -259,13 +259,25 @@ def collect_running_pids(pids): _gdi_screenshot = utilsdll.gdi_screenshot -_gdi_screenshot.argtypes = HWND, RECT, c_void_p +_gdi_screenshot.argtypes = HWND, c_void_p -crop_image = utilsdll.crop_image -crop_image.argtypes = HWND, RECT, c_void_p +_crop_image = utilsdll.crop_image +_crop_image.argtypes = HWND, RECT, c_void_p -def __gdi_screenshot(f, x1, y1, x2, y2, hwnd=None): +def gdi_screenshot(hwnd): + ret = [] + + def cb(ptr, size): + ret.append(cast(ptr, POINTER(c_char))[:size]) + + _gdi_screenshot(hwnd, CFUNCTYPE(None, c_void_p, c_size_t)(cb)) + if len(ret) == 0: + return None + return ret[0] + + +def crop_image(x1, y1, x2, y2, hwnd=None): rect = RECT() rect.left = x1 rect.top = y1 @@ -276,15 +288,12 @@ def __gdi_screenshot(f, x1, y1, x2, y2, hwnd=None): def cb(ptr, size): ret.append(cast(ptr, POINTER(c_char))[:size]) - f(hwnd, rect, CFUNCTYPE(None, c_void_p, c_size_t)(cb)) + _crop_image(hwnd, rect, CFUNCTYPE(None, c_void_p, c_size_t)(cb)) if len(ret) == 0: return None return ret[0] -gdi_screenshot = functools.partial(__gdi_screenshot, _gdi_screenshot) -crop_image = functools.partial(__gdi_screenshot, crop_image) - maximum_window = utilsdll.maximum_window maximum_window.argtypes = (HWND,) setbackdropX = utilsdll.setbackdropX