【问题标题】:How can you get the handle of a chrome process in java using JNA如何使用 JNA 在 java 中获取 chrome 进程的句柄
【发布时间】:2020-12-14 20:27:56
【问题描述】:

我是 JNA 的新手,我需要使用 java 和 JNA 截取 chrome 窗口的屏幕截图,我已经拥有使用此代码截取给定窗口的功能:

  public BufferedImage capture(WinDef.HWND hWnd) {

    WinDef.HDC hdcWindow = User32.INSTANCE.GetDC(hWnd);
    WinDef.HDC hdcMemDC = GDI32.INSTANCE.CreateCompatibleDC(hdcWindow);



    WinDef.RECT bounds = new WinDef.RECT();
    User32Extra.INSTANCE.GetClientRect(hWnd, bounds);

    int width = bounds.right - bounds.left;
    int height = bounds.bottom - bounds.top;

    WinDef.HBITMAP hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcWindow, width, height);

    WinNT.HANDLE hOld = GDI32.INSTANCE.SelectObject(hdcMemDC, hBitmap);
    GDI32Extra.INSTANCE.BitBlt(hdcMemDC, 0, 0, width, height, hdcWindow, 0, 0, WinGDIExtra.SRCCOPY);

    GDI32.INSTANCE.SelectObject(hdcMemDC, hOld);
    GDI32.INSTANCE.DeleteDC(hdcMemDC);

    WinGDI.BITMAPINFO bmi = new WinGDI.BITMAPINFO();
    bmi.bmiHeader.biWidth = width;
    bmi.bmiHeader.biHeight = -height;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = WinGDI.BI_RGB;

    Memory buffer = new Memory(width * height * 4);
    GDI32.INSTANCE.GetDIBits(hdcWindow, hBitmap, 0, height, buffer, bmi, WinGDI.DIB_RGB_COLORS);

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    image.setRGB(0, 0, width, height, buffer.getIntArray(0, width * height), 0, width);

    GDI32.INSTANCE.DeleteObject(hBitmap);
    User32.INSTANCE.ReleaseDC(hWnd, hdcWindow);

    return image;

}

然后我使用下面的代码来保存bufferedImage:

     BufferedImage image;

     WinDef.HWND hWnd = User32.INSTANCE.FindWindow(null, "notepad - untitled");
                image = capture(hWnd);
                File outputfile = new File("image.jpg");
                try {
                    ImageIO.write(image, "jpg", outputfile);
                } catch (IOException e) {
                    e.printStackTrace();
                }

这适用于某些应用程序,例如记事本,但不适用于 chrome,对于 chrome,它只会引发 Illegal Argument Exception,因为程序正在尝试捕获宽度和高度基本上为 0 的屏幕截图,因为未找到该进程,完整的例外是:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Allocation size must be greater than zero
at com.sun.jna.Memory.<init>(Memory.java:111)
at de.xliquid.stadiarpc.Main.capture(Main.java:103)
at de.xliquid.stadiarpc.Main$1.run(Main.java:56)
at com.sun.javafx.scene.KeyboardShortcutsHandler.processAccelerators(KeyboardShortcutsHandler.java:347)
at com.sun.javafx.scene.KeyboardShortcutsHandler.dispatchBubblingEvent(KeyboardShortcutsHandler.java:163)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$KeyHandler.process(Scene.java:3964)
at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3910)
at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2501)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$357(GlassViewEventHandler.java:248)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
at com.sun.glass.ui.View.notifyKey(View.java:966)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

我尝试使用 chrome 的当前标题,但没有奏效,我认为最好的解决方案是获取 chrome 的第一个选项卡的句柄。可悲的是,我真的不知道从哪里开始。谁能告诉我我能做什么?提前致谢。

【问题讨论】:

  • 我赞扬你的勇气,因为这个 JNA.. 它没有破坏你的机器吗??
  • 是的,这段代码虽然不是最好的解决方案,但目前可以使用。

标签: java google-chrome process jna hwnd


【解决方案1】:

JNA 的WindowUtils 课程在这里可以提供帮助。 getAllWindows() 方法将返回一个 List&lt;DesktopWindow&gt;,其中每个 DesktopWindow 实例都包含一个标题(与您的 Chrome 进程匹配)和该窗口的 HWND 句柄。

DesktopWindow 也有一个Rectangle 带有位置和大小尺寸,因此您可以从桌面本身复制适当的尺寸,例如这个简单的单行:

BufferedImage screenShot = new Robot().createScreenCapture(rectangle);

this StackOverlow Question 的回答确定了用于截取整个窗口的多个其他选项;只需适当地替换您从DesktopWindow 类派生的Rectangle(或使用您自己的代码与矩形的尺寸)。

或者,如果您不需要来自WindowUtils 类的所有信息,您可以更直接地使用WinAPI 的EnumWindows 函数来直接迭代窗口并仅捕获您需要的信息。这是WindowUtils 内部使用的函数,因此它的源代码将是一个很好的简化起点。

【讨论】:

  • “这个 StackOoverlow 问题的答案确定了用于截取整个窗口的屏幕截图的多个其他选项”:不要告诉我他们都依赖机器人!
  • 不,他们不是。那只是一个方便的单线。 :) 另外,“糟糕”的错字。
猜你喜欢
  • 2012-02-01
  • 1970-01-01
  • 2023-01-26
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-12
相关资源
最近更新 更多