需要制作一些游戏的屏幕截图。发现这个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);
}
}
答案 0 :(得分:0)
使用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
,您还可以截取部分屏幕的截图。
答案 1 :(得分:0)
GDI32Util.getScreenshot(HWND hwnd)
已经在jna中提供了方法。
但我的情况和你一样......游戏画面是黑色......没什么......