3

我正在使用以下代码截取屏幕截图:

        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRectangle);
        ImageIO.write(image,"png", file);

这段代码运行良好。当我运行程序等时,它会为我的桌面截屏。

但是,当我尝试先全屏运行一些游戏、应用程序时,它无法正常工作。它要么呈现黑屏,要么呈现相同的“初始”画面。

这个问题已知吗,请问如何解决?

第二个问题: 这是否可以模拟一些我们可以在键盘上使用的“打印屏幕”键来解决这个问题?因为当应用程序全屏启动时我可以使用Print Screen 键,当我粘贴时我得到了我的屏幕。
(像这样,但在 java 中:复制屏幕或活动窗口的内容?)

预先感谢您的帮助。

编辑:Andrew Thompson 很乐意向我推荐以下代码,这将是解决此问题的良好第一步:

import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.KeyEvent;

public class ScreenshotUsingPrintScreen {

public static void main(String[] args) throws Exception {
    int i = KeyEvent.VK_PRINTSCREEN;
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    DataFlavor[] flavors = cb.getAvailableDataFlavors();
    System.out.println("Before: ");
    for (DataFlavor flavor : flavors) {
        System.out.println(flavor);
    }

    // get the screenshot
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_PRINTSCREEN);
    robot.delay(40);
    robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
    robot.delay(40);

    cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    flavors = cb.getAvailableDataFlavors();
    System.out.println("After: ");
    for (DataFlavor flavor : flavors) {
        System.out.println(flavor);
    }
}

}

它编译得很好,但是我在运行它时收到以下错误。

Before: 
java.awt.datatransfer.DataFlavor[mimetype=application/x-java-text-encoding;representationclass=[B]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.io.Reader]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.lang.String]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.nio.CharBuffer]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=[C]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.io.InputStream;charset=UTF-16]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.nio.ByteBuffer;charset=UTF-16]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=[B;charset=UTF-16]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.io.InputStream;charset=UTF-8]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.nio.ByteBuffer;charset=UTF-8]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=[B;charset=UTF-8]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.io.InputStream;charset=UTF-16BE]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.nio.ByteBuffer;charset=UTF-16BE]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=[B;charset=UTF-16BE]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.io.InputStream;charset=UTF-16LE]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.nio.ByteBuffer;charset=UTF-16LE]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=[B;charset=UTF-16LE]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.io.InputStream;charset=ISO-8859-1]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.nio.ByteBuffer;charset=ISO-8859-1]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=[B;charset=ISO-8859-1]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.io.InputStream;charset=US-ASCII]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=java.nio.ByteBuffer;charset=US-ASCII]
java.awt.datatransfer.DataFlavor[mimetype=text/html;representationclass=[B;charset=US-ASCII]
java.awt.datatransfer.DataFlavor[mimetype=application/x-java-serialized-object;representationclass=java.lang.String]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.Reader]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.lang.String]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.CharBuffer]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[C]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=unicode]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=UTF-16]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=UTF-16]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=UTF-8]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=UTF-8]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=UTF-8]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=UTF-16BE]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=UTF-16BE]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=UTF-16BE]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=UTF-16LE]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=UTF-16LE]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=UTF-16LE]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=ISO-8859-1]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=ISO-8859-1]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=ISO-8859-1]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=US-ASCII]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=US-ASCII]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=US-ASCII]
Exception in thread "main" java.lang.IllegalStateException: cannot open system clipboard
    at sun.awt.windows.WClipboard.openClipboard(Native Method)
    at sun.awt.datatransfer.SunClipboard.getClipboardFormatsOpenClose(SunClipboard.java:332)
    at sun.awt.datatransfer.SunClipboard.getAvailableDataFlavors(SunClipboard.java:172)
    at zipprotected.threadsTest.ScreenshotUsingPrintScreen.main(ScreenshotUsingPrintScreen.java:36)
Java Result: 1
4

2 回答 2

4

这个问题知道吗,..

是的。通常它是由将渲染下移到显卡本身的级别(主要是避开操作系统)引起的。

..以及如何解决这个问题?

AFAIU 它无法修复。甚至是原生截图应用。将无法获得此类渲染的屏幕截图。

这是否可以模拟一些“打印屏幕”键来解决这个问题?

import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.KeyEvent;

public class ScreenshotUsingPrintScreen {

    public static void main(String[] args) throws Exception {
        int i = KeyEvent.VK_PRINTSCREEN;
        Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
        DataFlavor[] flavors = cb.getAvailableDataFlavors();
        System.out.println("Before: ");
        for (DataFlavor flavor : flavors) {
            System.out.println(flavor);
        }

        // get the screenshot
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_PRINTSCREEN);
        robot.delay(40);
        robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
        robot.delay(40);

        cb = Toolkit.getDefaultToolkit().getSystemClipboard();
        flavors = cb.getAvailableDataFlavors();
        System.out.println("After: ");
        for (DataFlavor flavor : flavors) {
            System.out.println(flavor);
        }
    }
}

输出

Before: 
java.awt.datatransfer.DataFlavor[mimetype=application/x-java-text-encoding;representationclass=[B]
java.awt.datatransfer.DataFlavor[mimetype=text/rtf;representationclass=java.io.InputStream]
java.awt.datatransfer.DataFlavor[mimetype=text/rtf;representationclass=java.nio.ByteBuffer]
java.awt.datatransfer.DataFlavor[mimetype=text/rtf;representationclass=[B]
java.awt.datatransfer.DataFlavor[mimetype=application/x-java-serialized-object;representationclass=java.lang.String]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.Reader]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.lang.String]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.CharBuffer]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[C]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=unicode]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=UTF-16]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=UTF-16]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=UTF-8]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=UTF-8]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=UTF-8]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=UTF-16BE]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=UTF-16BE]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=UTF-16BE]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=UTF-16LE]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=UTF-16LE]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=UTF-16LE]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=ISO-8859-1]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=ISO-8859-1]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=ISO-8859-1]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=windows-1252]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=windows-1252]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=windows-1252]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.io.InputStream;charset=US-ASCII]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=java.nio.ByteBuffer;charset=US-ASCII]
java.awt.datatransfer.DataFlavor[mimetype=text/plain;representationclass=[B;charset=US-ASCII]
After: 
java.awt.datatransfer.DataFlavor[mimetype=image/x-java-image;representationclass=java.awt.Image]
于 2012-09-03T16:33:34.187 回答
1

你的问题的解决方案就在你面前,尝试不同的想法。您不需要使用PrtScr功能,因为几乎所有平台(游戏)都具有内置的屏幕截图功能。

以 Steam 为例,这包括所有通过 Steam 玩的游戏,F12用于截屏。而不是在您的代码中使用PrtScr, use ,而是F12自动存储该文件,使用该文件,随心所欲地使用它。

我知道这有点晚了,但我希望它对您的项目有所帮助。

编辑以添加确切的代码:

if (e.getKeyCode() == NativeKeyEvent.VC_0) {
    Robot robot = null;
    try {
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_F12);
        robot.keyRelease(KeyEvent.VK_F12);
    } 
    catch (AWTException ex) {
        Logger.getLogger(JavaApplication6.class.getName()).log(Level.SEVERE, null, ex);
    }
}

这和steam有关,基本上当你按“0”时,你的程序会模拟输入F12,这会截图,保存在"STEAM\userdata\137637106\760\remote\570\screenshots"目录中。

希望这可以帮助。

于 2014-12-06T07:16:29.157 回答