2
Rubyを使って画面をキャプチャし、画面上の各ピクセルのRGBピクセル値の配列を取得する必要があります。Rubyを使用して画面をキャプチャするにはどうすればよいですか?
私はWindows APIを試してみましたが、画面をbitbltしてビットマップのハンドルを取得できましたが、このハンドルのデータ内の生のRGB値にアクセスする方法はわかりません。
これは私が現時点で持っているものですが、十分に速いですが、hビットマップからRGB値を取り出す必要があります。
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
ちょっと、ルビーがwin32APIに同梱されていることを知らなかった。正確には解決策ではありませんが、これは役立ちます:http://www.ruby-forum.com/topic/166943 –
ああ、これもhttp://stackoverflow.com/questions/1678890/how-to-capture-a-画面の一部を使って画面上にルビーを表示する –
私は上記のスタックフローリンクを試してみましたが、1.9.3で私を納得させることはできません – marscom