简体   繁体   English

使用 JNA 获取单个像素的颜色

[英]Get the colour of a single pixel using JNA

I'm trying to get the colour of a pixel on the screen, but I can't find a method that has a sustainable tick rate.我正在尝试获取屏幕上像素的颜色,但找不到具有可持续滴答率的方法。

So the first option I tried is the Robot class in Java - it's simple to use, but it just simply isn't fast enough.所以我尝试的第一个选项是 Java 中的 Robot 类 - 它使用简单,但速度不够快。

I then found a way of capturing screen shots using JNA from this post .然后我从这篇文章中找到了一种使用 JNA 捕获屏幕截图的方法。 After testing and playing around with the code a bit, I found that I'm able to almost get what I wanted, mostly by modifying this line:在稍微测试和玩弄代码之后,我发现我几乎可以得到我想要的东西,主要是通过修改这一行:

GDI32Extra.INSTANCE.BitBlt(hdcMemDC, 0, 0, width, height, hdcWindow, 0, 0, WinGDIExtra.SRCCOPY);

The only issue is that it still captures the whole window before picking out the single pixel that you want and it just doesn't seem right.唯一的问题是它在挑选出您想要的单个像素之前仍会捕获整个窗口,并且看起来不正确。 This slows down the tick rate by quite a bit.这会大大降低滴答速率。

Is there any way I can capture just a single pixel, or am I forced to always capture an entire window first?有什么办法可以只捕获一个像素,还是必须先捕获整个窗口?

I'm pretty inexperienced with JNA's libraries so maybe I'm just missing something simple.我对 JNA 的库非常缺乏经验,所以也许我只是缺少一些简单的东西。 If anyone could point me in the right direction, it would be much appreciated.如果有人能指出我正确的方向,将不胜感激。

Thanks.谢谢。

Try adding the GetPixel function to your own JNA class.尝试将GetPixel函数添加到您自己的 JNA 类中。

public interface MyGDI32 extends com.sun.jna.platform.win32.GDI32 {
    MyGDI32 INSTANCE = Native.load("gdi32", MyGDI32.class, W32APIOptions.DEFAULT_OPTIONS);

    int GetPixel(HDC hdc, int x, int y);
}

Then call it with MyGDI32.INSTANCE.GetPixel() .然后用MyGDI32.INSTANCE.GetPixel()调用它。

I can't guarantee it'll be any faster than the Robot class, but it does answer your question how to "Get the colour of a single pixel using JNA."我不能保证它会比Robot类更快,但它确实回答了您如何“使用 JNA 获取单个像素的颜色”的问题。

You'll have to parse the DWORD (32 bit int) return value which is in COLORREF format: 0x00bbggrr .您必须解析COLORREF格式的DWORD (32 位整数)返回值: 0x00bbggrr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM