use*_*875 4 java netbeans bufferedimage printscreen awtrobot
我知道我们可以使用以下代码模拟打印屏幕:
robot.keyPress(KeyEvent.VK_PRINTSCREEN);
Run Code Online (Sandbox Code Playgroud)
..但那么如何归还一些BufferedImage?
我在Google上找到了一些调用方法,getClipboard()但Netbeans在这个方法上给我一些错误(找不到符号).
我很遗憾地问这个,但是有人可以告诉我一个关于如何从这个按键返回的工作代码,BufferedImage然后我可以保存吗?
这不一定会给你一个BufferedImage,但它会是一个Image.这利用了Toolkit.getSystemClipboard.
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
if (clipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)) {
final Image screenshot = (Image) clipboard.getData(DataFlavor.imageFlavor);
...
}
Run Code Online (Sandbox Code Playgroud)
如果你真的需要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) { }
}
}
Run Code Online (Sandbox Code Playgroud)
虽然,我真的不得不问为什么你不只是使用Robot.createScreenCapture.
final Robot robot = new Robot();
final GraphicsConfiguration config
= GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration();
final BufferedImage screenshot = robot.createScreenCapture(config.getBounds());
Run Code Online (Sandbox Code Playgroud)