我需要使用 Ruby 捕获屏幕,然后获取屏幕上每个像素的 RGB 像素值数组。
我尝试了 Windows API,并且可以对屏幕进行位转换并检索位图的句柄,但我不知道如何访问该句柄数据中的原始 RGB 值。
这就是我目前所拥有的,它足够快,但我需要将 RGB 值从 hbitmap 获取到一个我可以使用的数组中。
任何与 Bitblt 一样快但更容易的东西也会受到赞赏。
def getscreen()
width = Win32API.new("User32.dll","GetSystemMetrics",["L"],"L").call(0)
height = Win32API.new("User32.dll","GetSystemMetrics",["L"],"L").call(1)
#Get desktop DC, create a compatible dc, create a comaptible bitmap and select into compatible dc.
hddc = Win32API.new("User32.dll","GetDC",["L"],"L").call(Win32API.new("User32.dll","GetDesktopWindow",[],"L").call)
hcdc = Win32API.new("Gdi32.dll","CreateCompatibleDC",["L"],"L").call(hddc)
hbitmap = Win32API.new("Gdi32.dll","CreateCompatibleBitmap",["L","L","L"],"L").call(hddc,width,height)
Win32API.new("Gdi32.dll","SelectObject",["L","L"],"L").call(hcdc,hbitmap)
Win32API.new("Gdi32.dll","BitBlt",["L","L","L","L","L","L","L","L","P"],"L").call(hcdc,0,0,width,height,hddc,0,0,"SRCCOPY|CAPTUREBLT")
#save hbitmap to stream of byte as you mentioned
puts hbitmap
#
Win32API.new("User32.dll","ReleaseDC",["L","L"],"L").call(Win32API.new("User32.dll","GetDesktopWindow",[],"L").call,hddc)
Win32API.new("Gdi32.dll","DeleteDC",["L"],"L").call(hcdc)
Win32API.new("Gdi32.dll","DeleteObject",["L"],"L").call(hbitmap)
#Print screen width and height
puts "Screen width: #{width}"
puts "Screen height: #{height}"
end
最佳答案
我想出了如何做到这一点,所以我会为可能需要帮助的其他人发布解决方案。
GetDIBits Win32API 函数可用于访问存储在使用上述代码捕获的位图中的 RGB 数组。
http://msdn.microsoft.com/en-us/library/dd144879%28v=vs.85%29.aspx
关于ruby - 如何使用 Ruby 捕获屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8685068/