使用python ploblem捕获隐藏或最小化的窗口

时间:2020-11-08 13:17:37

标签: python windows opencv

嗨,我想捕获并流化隐藏或最小化的窗口。 有关使用c ++的问题的一些文档。 所以我从这个页面开始。

https://docs.microsoft.com/ko-kr/windows/win32/gdi/capturing-an-image

import ctypes
from ctypes import wintypes, windll
import win32api
import win32ui
import win32gui
import win32con
import numpy as np
import cv2

class BITMAPINFOHEADER(ctypes.Structure):
    _fields_ = [
        ('biSize', wintypes.DWORD),
        ('biWidth', wintypes.LONG),
        ('biHeight', wintypes.LONG),
        ('biPlanes', wintypes.WORD),
        ('biBitCount', wintypes.WORD),
        ('biCompression', wintypes.DWORD),
        ('biSizeImage', wintypes.DWORD),
        ('biXPelsPerMeter', wintypes.LONG),
        ('biYPelsPerMeter', wintypes.LONG),
        ('biClrUsed', wintypes.DWORD),
        ('biClrImportant', wintypes.DWORD)
    ]

def getScreen(hwnd):
    t1 = cv2.getTickCount()
    left, top, right, bot = win32gui.GetClientRect(hwnd)
    width = (right - left)
    height = (bot - top)
    #width = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
    #height = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)

    #targetDC = win32gui.GetDC(win32con.NULL)
    targetDC = win32gui.GetDC(hwnd)
    compatibleDC = win32gui.CreateCompatibleDC(targetDC)

    win32gui.SetStretchBltMode(compatibleDC, win32con.COLORONCOLOR)

    bitCount = win32ui.GetDeviceCaps(targetDC, win32con.BITSPIXEL)
    #img = np.zeros((height, width, 4 if bitCount == 32 else 3), np.uint8)
    img = np.zeros((height, width, 4), np.uint8)
    bitmap = win32gui.CreateCompatibleBitmap(targetDC, width, height)

    bitmapInfo = BITMAPINFOHEADER()
    bitmapInfo.biSize = ctypes.sizeof(BITMAPINFOHEADER)
    bitmapInfo.biWidth = width
    bitmapInfo.biHeight = -height
    bitmapInfo.biPlanes = 1
    bitmapInfo.biCompression = win32con.BI_RGB
    bitmapInfo.biSizeImage = 0
    bitmapInfo.biXPelsPerMeter = 0
    bitmapInfo.biYPelsPerMeter = 0
    bitmapInfo.biClrUsed = 0
    bitmapInfo.biClrImportant = 0
    bitmapInfo.biBitCount = bitCount

    win32gui.SelectObject(compatibleDC, bitmap.handle)
    #windll.gdi32.BitBlt(compatibleDC, 0, 0, width, height, targetDC, 0, 0, win32con.SRCCOPY)
    win32gui.BitBlt(compatibleDC, 0, 0, width, height, targetDC, 0, 0, win32con.SRCCOPY)

    windll.gdi32.GetDIBits(compatibleDC, bitmap.handle, 0, height,
                     ctypes.cast(img.ctypes.data, ctypes.POINTER(ctypes.c_byte)),
                     ctypes.byref(bitmapInfo),
                     win32con.DIB_RGB_COLORS)

    win32gui.DeleteObject(bitmap)
    win32gui.DeleteDC(compatibleDC)
    win32gui.ReleaseDC(win32con.NULL, targetDC)

    t1 = (cv2.getTickCount() - t1) / cv2.getTickFrequency()
    print("Time : ", t1)
    return img



hwnd = win32gui.FindWindow("ZPPTMainFrmWndClassEx", "Zoom")
#cv2.namedWindow("output");

img = getScreen(hwnd)
cv2.imshow("output", img)
key = cv2.waitKey(0)    
cv2.destroyAllWindows()


我的问题:

  1. 当我一次捕获1张图片时,它可以工作,但是我不在while循环中工作,因此无法流式传输窗口。我应该修复哪一部分?
  2. 我也有解决问题。宽度,高度与原始来源匹配,但其分辨率与结果不匹配。我无法理解特定功能的含义,因此我通过调整参数进行测试,但是它不起作用。我应该调整哪个参数?

感谢阅读。

0 个答案:

没有答案