2017-09-27 7 views
-1

いくつかのゲームのスクリーンショットを作成する必要があります。このJNAコードを見つけましたが、私がスクリーンをしようとすると、私はちょうど黒い画面を取得します。私がワードパッドのようないくつかのプログラムの画面をしようとすると、それはうまく動作します。私は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

フルスクリーンのDirectX/OpenGLのゲームはあなたのGPUのドライバと直接通信しています。ウィンドウモードで動作していない限り、ゲームはフレームをWindowsのウィンドウに表示しません。 DirectX(またはOpenGL)を使用してスクリーンショットを取得するには、ゲーム自体にフックする必要があります:https://stackoverflow.com/q/1962142/996081 – cubrr

答えて

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あなたはまた、単に画面の一部のスクリーンショットを取ることができる調整すること。

+0

特定のdirectx \ OpenGlプログラムのscreeshotを実行する必要があります。私はこのオプションでは不可能だと思っています – Drop

+0

なぜ提供されたコードでは不可能だと思いますか? – cello

+0

デスクトップのスクリーンショットです。私が眠っているので。もしゲームが見えないなら、それは単にデスクトップスクリーンをやっているだけだ。 – Drop

0

GDI32Util.getScreenshot(HWNDのHWND)

方法は既にJNAに設けられています。

が、私の場合は、あなたと同じ....ゲーム画面が黒であるとして...何も...

+0

この情報をアップアップしたりコメントして共有することができます。回答セクションを使用してソリューションを提供する方が良いです。 – chaitan64arun