Get the colour of a single pixel using JNA

Jordan :

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.

I then found a way of capturing screen shots using JNA from this post. 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. If anyone could point me in the right direction, it would be much appreciated.

Thanks.

Daniel Widdis :

Try adding the GetPixel function to your own JNA class.

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().

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."

You'll have to parse the DWORD (32 bit int) return value which is in COLORREF format: 0x00bbggrr.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=158724&siteId=1