Java 在Windows下模拟打印屏幕

Java 在Windows下模拟打印屏幕,java,netbeans,bufferedimage,awtrobot,printscreen,Java,Netbeans,Bufferedimage,Awtrobot,Printscreen,我知道我们可以用以下代码模拟打印屏幕: robot.keyPress(KeyEvent.VK_PRINTSCREEN); …但是如何返回一些缓冲区图像 我在Google上找到了一个名为getClipboard()的方法,但Netbeans在这个方法上返回了一些错误(找不到符号) 很抱歉问这个问题,有人能告诉我一个工作代码,说明如何从这个键返回一个BufferedImage,然后我可以保存它吗?这不一定会给你一个BufferedImage,但它将是一个图像。这是利用 如果您确实需要缓冲区图像,

我知道我们可以用以下代码模拟打印屏幕:

 robot.keyPress(KeyEvent.VK_PRINTSCREEN);
…但是如何返回一些
缓冲区图像

我在Google上找到了一个名为
getClipboard()
的方法,但Netbeans在这个方法上返回了一些错误(找不到符号)


很抱歉问这个问题,有人能告诉我一个工作代码,说明如何从这个键返回一个
BufferedImage
,然后我可以保存它吗?

这不一定会给你一个
BufferedImage
,但它将是一个
图像。这是利用

如果您确实需要
缓冲区图像
,请尝试以下操作

final GraphicsConfiguration config
    = GraphicsEnvironment.getLocalGraphicsEnvironment()
          .getDefaultScreenDevice().getDefaultConfiguration();
final BufferedImage copy = config.createCompatibleImage(
    screenshot.getWidth(null), screenshot.getHeight(null));
final Object monitor = new Object();
final ImageObserver observer = new ImageObserver() {

  public void imageUpdate(final Image img, final int flags,
      final int x, final int y, final int width, final int height) {
    if ((flags & ALLBITS) == ALLBITS) {
      synchronized (monitor) {
        monitor.notifyAll();
      }
    }
  }
};
if (!copy.getGraphics().drawImage(screenshot, 0, 0, observer)) {
  synchronized (monitor) {
    try {
      monitor.wait();
    } catch (final InterruptedException ex) { }
  }
}
不过,我真的要问你为什么不直接使用


在将
图像
保存到剪贴板之前,您可能需要释放该键,但我不确定。另请参见前面的问题。别再冲我们大喊大叫了.“我真的得问.”好问题。很明显,OP假设我们都坐在一起,只关心他们的问题。前面的一个问题解释了
createscreenscapture
为一些全屏应用程序生成黑色图像。这也是一个很好的答案+首先,谢谢你的回答。第二,我真的不知道为什么,但当我运行你的代码时,我收到了以下错误:“严重:null java.lang.NullPointerException”我想它来自我的以下代码:“ImageIO.write(copy,“png”,file1);”,对吗?@user1638875我不知道,我没有写那段代码。异常的名称不足以帮助我。。。如果你有完整的线索。
final GraphicsConfiguration config
    = GraphicsEnvironment.getLocalGraphicsEnvironment()
          .getDefaultScreenDevice().getDefaultConfiguration();
final BufferedImage copy = config.createCompatibleImage(
    screenshot.getWidth(null), screenshot.getHeight(null));
final Object monitor = new Object();
final ImageObserver observer = new ImageObserver() {

  public void imageUpdate(final Image img, final int flags,
      final int x, final int y, final int width, final int height) {
    if ((flags & ALLBITS) == ALLBITS) {
      synchronized (monitor) {
        monitor.notifyAll();
      }
    }
  }
};
if (!copy.getGraphics().drawImage(screenshot, 0, 0, observer)) {
  synchronized (monitor) {
    try {
      monitor.wait();
    } catch (final InterruptedException ex) { }
  }
}
final Robot robot = new Robot();
final GraphicsConfiguration config
    = GraphicsEnvironment.getLocalGraphicsEnvironment()
          .getDefaultScreenDevice().getDefaultConfiguration();
final BufferedImage screenshot = robot.createScreenCapture(config.getBounds());