gpt4 book ai didi

ruby - 在 ruby​​ 中使用 GetDIBits 函数时遇到问题

转载 作者:可可西里 更新时间:2023-11-01 12:02:20 31 4
gpt4 key购买 nike

我有下面的 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

最佳答案

这段代码似乎得到了一些数据,虽然我没有仔细观察以判断它是否是来自屏幕的颜色:

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

如果您对我在这里所做的事情有任何疑问,请告诉我。

关于ruby - 在 ruby​​ 中使用 GetDIBits 函数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8691497/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com