Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
HIllya51 committed Jan 19, 2025
1 parent a4189fd commit b89ad72
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
3 changes: 2 additions & 1 deletion cpp/winsharedutils/screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ std::vector<byte> __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());
Expand Down
21 changes: 3 additions & 18 deletions py/LunaTranslator/myutils/hwnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

_()
Expand All @@ -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)

_()
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions py/LunaTranslator/myutils/ocrutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 17 additions & 8 deletions py/LunaTranslator/winsharedutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b89ad72

Please sign in to comment.