More screencapture
This commit is contained in:
51
owocr/run.py
51
owocr/run.py
@@ -999,7 +999,13 @@ class ScreenshotThread(threading.Thread):
|
|||||||
self.screencapturekit_queue.put(None)
|
self.screencapturekit_queue.put(None)
|
||||||
return
|
return
|
||||||
|
|
||||||
self.screencapturekit_queue.put(image)
|
with objc.autorelease_pool():
|
||||||
|
width = CGImageGetWidth(image)
|
||||||
|
height = CGImageGetHeight(image)
|
||||||
|
raw_data = CGDataProviderCopyData(CGImageGetDataProvider(image))
|
||||||
|
bpr = CGImageGetBytesPerRow(image)
|
||||||
|
img = Image.frombuffer('RGBA', (width, height), bytes(raw_data), 'raw', 'BGRA', bpr, 1)
|
||||||
|
self.screencapturekit_queue.put(img)
|
||||||
|
|
||||||
window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionIncludingWindow, window_id)
|
window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionIncludingWindow, window_id)
|
||||||
if not window_list or len(window_list) == 0:
|
if not window_list or len(window_list) == 0:
|
||||||
@@ -1083,26 +1089,19 @@ class ScreenshotThread(threading.Thread):
|
|||||||
return None
|
return None
|
||||||
if not self.window_visible:
|
if not self.window_visible:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
self.window_size_changed = False
|
|
||||||
if sys.platform == 'darwin':
|
if sys.platform == 'darwin':
|
||||||
with objc.autorelease_pool():
|
with objc.autorelease_pool():
|
||||||
if self.old_macos_screenshot_api:
|
if self.old_macos_screenshot_api:
|
||||||
cg_image = CGWindowListCreateImageFromArray(CGRectNull, [self.window_id], kCGWindowImageBoundsIgnoreFraming | kCGWindowImageNominalResolution)
|
cg_image = CGWindowListCreateImageFromArray(CGRectNull, [self.window_id], kCGWindowImageBoundsIgnoreFraming | kCGWindowImageNominalResolution)
|
||||||
|
width = CGImageGetWidth(cg_image)
|
||||||
|
height = CGImageGetHeight(cg_image)
|
||||||
|
raw_data = CGDataProviderCopyData(CGImageGetDataProvider(cg_image))
|
||||||
|
bpr = CGImageGetBytesPerRow(cg_image)
|
||||||
|
img = Image.frombuffer('RGBA', (width, height), bytes(raw_data), 'raw', 'BGRA', bpr, 1)
|
||||||
else:
|
else:
|
||||||
cg_image = self.capture_macos_window_screenshot(self.window_id)
|
img = self.capture_macos_window_screenshot(self.window_id)
|
||||||
if not cg_image:
|
if not img:
|
||||||
return False
|
return False
|
||||||
width = CGImageGetWidth(cg_image)
|
|
||||||
height = CGImageGetHeight(cg_image)
|
|
||||||
raw_data = CGDataProviderCopyData(CGImageGetDataProvider(cg_image))
|
|
||||||
bpr = CGImageGetBytesPerRow(cg_image)
|
|
||||||
current_size = (width, height)
|
|
||||||
if self.window_size != current_size:
|
|
||||||
if self.window_size:
|
|
||||||
self.window_size_changed = True
|
|
||||||
self.window_size = current_size
|
|
||||||
img = Image.frombuffer('RGBA', (width, height), raw_data, 'raw', 'BGRA', bpr, 1)
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
coord_left, coord_top, right, bottom = win32gui.GetWindowRect(self.window_handle)
|
coord_left, coord_top, right, bottom = win32gui.GetWindowRect(self.window_handle)
|
||||||
@@ -1111,32 +1110,28 @@ class ScreenshotThread(threading.Thread):
|
|||||||
|
|
||||||
current_size = (coord_width, coord_height)
|
current_size = (coord_width, coord_height)
|
||||||
if self.window_size != current_size:
|
if self.window_size != current_size:
|
||||||
if self.window_size:
|
self.cleanup_window_screen_capture()
|
||||||
window_size_changed = True
|
|
||||||
self.reset_windows_window()
|
|
||||||
|
|
||||||
hwnd_dc = win32gui.GetWindowDC(self.window_handle)
|
hwnd_dc = win32gui.GetWindowDC(self.window_handle)
|
||||||
self.windows_window_mfc_dc = win32ui.CreateDCFromHandle(hwnd_dc)
|
self.windows_window_mfc_dc = win32ui.CreateDCFromHandle(hwnd_dc)
|
||||||
self.windows_window_save_dc = self.windows_window_mfc_dc.CreateCompatibleDC()
|
self.windows_window_save_dc = self.windows_window_mfc_dc.CreateCompatibleDC()
|
||||||
self.windows_window_save_bitmap = win32ui.CreateBitmap()
|
self.windows_window_save_bitmap = win32ui.CreateBitmap()
|
||||||
self.windows_window_save_bitmap.CreateCompatibleBitmap(self.windows_window_mfc_dc, coord_width, coord_height)
|
self.windows_window_save_bitmap.CreateCompatibleBitmap(self.windows_window_mfc_dc, coord_width, coord_height)
|
||||||
self.windows_window_save_dc.SelectObject(self.windows_window_save_bitmap)
|
self.windows_window_save_dc.SelectObject(self.windows_window_save_bitmap)
|
||||||
|
|
||||||
self.window_size = current_size
|
|
||||||
win32gui.ReleaseDC(self.window_handle, hwnd_dc)
|
win32gui.ReleaseDC(self.window_handle, hwnd_dc)
|
||||||
|
|
||||||
result = ctypes.windll.user32.PrintWindow(self.window_handle, self.windows_window_save_dc.GetSafeHdc(), 2)
|
result = ctypes.windll.user32.PrintWindow(self.window_handle, self.windows_window_save_dc.GetSafeHdc(), 2)
|
||||||
|
|
||||||
bmpinfo = self.windows_window_save_bitmap.GetInfo()
|
bmpinfo = self.windows_window_save_bitmap.GetInfo()
|
||||||
bmpstr = self.windows_window_save_bitmap.GetBitmapBits(True)
|
bmpstr = self.windows_window_save_bitmap.GetBitmapBits(True)
|
||||||
|
|
||||||
img = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1)
|
img = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1)
|
||||||
return img
|
|
||||||
except pywintypes.error:
|
except pywintypes.error:
|
||||||
return None
|
return False
|
||||||
|
window_size_changed = False
|
||||||
|
if self.window_size != img.size:
|
||||||
|
if self.window_size:
|
||||||
|
window_size_changed = True
|
||||||
|
self.window_size = img.size
|
||||||
if self.window_area_coordinates:
|
if self.window_area_coordinates:
|
||||||
if self.window_size_changed:
|
if window_size_changed:
|
||||||
self.window_area_coordinates = None
|
self.window_area_coordinates = None
|
||||||
logger.warning('Window size changed, discarding area selection')
|
logger.warning('Window size changed, discarding area selection')
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user