我有下面的Ruby代码,我正在使用Windows API调用,我应该能够使用函数GetDIBits从我创建的位图中检索RGB值数组,以用于AI处理。我需要为GetDIBits函数提供BITMAPINFO结构以及一些其他变量。我应该对其他变量没问题,但是如何在ruby中创建一个可以用于此函数的结构(使用Windows API)?
请参阅以下代码
如果您可以完成GetDIBits功能以便我可以检索RGB值数组,我将非常感激。
由于 马丁
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
Win32API.new("Gdi32.dll","GetDIBits",["L","L","L","L","L","L","P"],"L").call(hcdc,hbitmap,1,1200,0,"DIB_RGB_COLORS")
pixelarray = Array.new
#Need a .call to fill the pixelarray array
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
答案 0 :(得分:2)
这段代码似乎得到了一些数据,虽然我没有仔细查看它是否是屏幕上的颜色:
require 'Win32API'
module Foo
GetSystemMetrics = Win32API.new "User32.dll", "GetSystemMetrics", ["L"], "L"
GetDC = Win32API.new "User32.dll", "GetDC", ["L"], "L"
GetDesktopWindow = Win32API.new "User32.dll", "GetDesktopWindow", [], "L"
CreateCompatibleDC = Win32API.new "Gdi32.dll", "CreateCompatibleDC", ["L"], "L"
CreateCompatibleBitmap = Win32API.new "Gdi32.dll", "CreateCompatibleBitmap", ["L","L","L"], "L"
SelectObject = Win32API.new "Gdi32.dll","SelectObject", ["L","L"], "L"
BitBlt = Win32API.new "Gdi32.dll","BitBlt", ["L","L","L","L","L","L","L","L","L"], "L"
CAPTUREBLT = 0x40000000
SRCCOPY = 0x00CC0020
GetDIBits = Win32API.new "Gdi32.dll","GetDIBits", ["L","L","L","L","P","P","L"], "L"
DIB_RGB_COLORS = 0
BI_RGB = 0
def self.getscreen
width = GetSystemMetrics.call 0
height = GetSystemMetrics.call 1
puts "Screen width: #{width}"
puts "Screen height: #{height}"
desktop_handle = GetDesktopWindow.call
raise "GetDesktopWindow failed" if desktop_handle == 0
hddc = GetDC.call desktop_handle
raise "Get DC failed." if hddc == 0
hcdc = CreateCompatibleDC.call hddc
raise "CreateCompatibleDC failed" if hcdc == 0
hbitmap = CreateCompatibleBitmap.call hddc, width, height
raise "CreateCompatibleBitmap failed" if hbitmap == 0
result = SelectObject.call hcdc, hbitmap
raise "SelectObject failed" if result == 0 # not sure if this is right
result = BitBlt.call hcdc, 0, 0, width, height, hddc, 0, 0, SRCCOPY | CAPTUREBLT
raise "BitBlt failed" if result == 0
SelectObject.call hcdc, 0 # attempt to deselect the bitmap because GetDIBits requires it?
size_in_bytes = width*height*4
bitmap_info = [40, #biSize
width,
height,
1, #biPlanes
32, #biBitCount
BI_RGB, #biCompression
size_in_bytes,
2000, #biXPelsPerMeter: not used?
2000, #biYPelsPerMeter: not used?
0, # biClrUsed
0 # biClrImportant
].pack("LLLSSLLLLLL") #+ [0,0,0,0].pack("LLLL")
buffer = (" " * size_in_bytes).force_encoding("BINARY")
result = GetDIBits.call hcdc, hbitmap, 0, height, buffer, bitmap_info, DIB_RGB_COLORS
raise "GetDIBits failed." if result == 0
puts "Number of scan lines copied: #{result}"
puts "buffer contents: " + buffer.strip.inspect[0,100] + " ..."
puts "First pixel = %02x,%02x,%02x,%02x" % [buffer[0].ord, buffer[1].ord, buffer[2].ord, buffer[3].ord]
return buffer
end
end
Foo.getscreen
在Windows中使用ruby 1.9.2p180运行它时的输出是:
Screen width: 1366
Screen height: 768
Number of scan lines copied: 768
buffer contents: "\x0F\f\a\xFF\x0F\f\a\xFF\x0F\f\a\xFF\x0F\f\a\xFF\x0F\f\a\xFF\x
0F\f\a\xFF\x0F\f\a\xFF\x0F\f\a\xFF\x0 ...
First pixel = 0f,0c,07,ff
如果您对我在这里做的事情有任何疑问,请告诉我。