【问题标题】:JNA screenshot gameJNA截图游戏
【发布时间】:2017-09-28 18:44:32
【问题描述】:

需要制作一些游戏的截图。找到了这个 JNA 代码,但是当我尝试做屏幕时,我只是得到黑屏。当我尝试做一些程序的屏幕时,比如写字板等等,它工作得很好。还有我JNA不好,想请教一下。有没有可能完成这个任务?

public class Paint extends JFrame {
public BufferedImage capture(HWND hWnd) throws IOException {
    String gettime = Gettime.screentime();
    HDC hdcWindow = User32.INSTANCE.GetDC(hWnd);
    HDC hdcMemDC = GDI32.INSTANCE.CreateCompatibleDC(hdcWindow);
    RECT bounds = new RECT();
    User32Extra.INSTANCE.GetClientRect(hWnd, bounds);
    int width = bounds.right - bounds.left;
    int height = bounds.bottom - bounds.top;

    HBITMAP hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcWindow, width, height);
    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);
    BITMAPINFO bmi = new 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);
    File outputfile = new File("C:\\image" +gettime+ ".jpg");
    ImageIO.write(image, "jpg", outputfile);
    return image;
}

public static void main(String[] args) throws IOException {

        new Paint();

}
BufferedImage image;

public Paint() throws IOException {
    HWND hWnd = User32.INSTANCE.FindWindow(null, "some game");
    this.image = capture(hWnd);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setExtendedState(MAXIMIZED_BOTH);
    setVisible(true);
}
@Override
public void paint(Graphics g) {
    super.paint(g);
    g.drawImage(image, 20, 40, null);
}
}

【问题讨论】:

  • 全屏 DirectX/OpenGL 游戏直接与您的 GPU 驱动程序通信。除非您在窗口模式下运行,否则游戏不会在 Windows Window 上渲染帧。您需要连接到游戏本身才能通过 DirectX(或 OpenGL)获取屏幕截图:stackoverflow.com/q/1962142/996081

标签: java jna


【解决方案1】:

使用 JNA 截屏听起来非常复杂,而且与平台无关。 Java 具有使用Robot 类截取屏幕截图的内置功能:

import java.awt.Robot;

Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "png", new File("./screenshot.png"));

通过调整screenRect,您也可以只截取屏幕的一部分。

【讨论】:

  • 我需要对一个特定的 directx\OpenGl 程序进行截图。我认为这个选项是不可能的
  • 为什么你认为提供的代码是不可能的?
  • 是做桌面截图。据我所知。如果游戏没有t visible, its 还是做桌面屏幕。
  • 不可见游戏的截图应该是什么?我很困惑。也许这就是您的屏幕截图为黑色的原因,因为窗口尚未显示?
  • 当游戏不可见时,我什么都不需要。我不想为用户进行间谍活动。他们的桌面对我来说并不有趣。即使游戏可见,它也是黑色的。只要游戏在窗口中运行,我就能得到一张图片。
【解决方案2】:

GDI32Util.getScreenshot(HWND hwnd)

jna中已经提供了方法。

但我的情况和你一样……游戏画面是黑的……什么都没有……

【讨论】:

  • 您可以投票或评论以分享此信息。最好使用答案部分来提供解决方案。
猜你喜欢
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-12
  • 1970-01-01
  • 2020-09-01
  • 2020-11-12
相关资源
最近更新 更多