[适用于Windows] 我知道可以捕获在C#语言下运行的DirectX程序的屏幕,但是你知道一些Java的示例代码吗?
我实际上遇到了与此Take screen shots inside of full screen applications with java?相同的问题。机器人课没有帮助,也没有工作。
但是我没有在互联网上找到任何有关此问题的java代码示例。 感谢您就此主题提供的任何帮助。
答案 0 :(得分:2)
自从我更多地工作以来,请参阅:
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.io.File;
public class ClipboardScreenshot {
public static void main(String[] args) throws Exception {
// get the screenshot
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_PRINTSCREEN);
robot.delay(40);
robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
robot.delay(404);
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
DataFlavor[] flavors = cb.getAvailableDataFlavors();
System.out.println("After: ");
for (DataFlavor flavor : flavors) {
System.out.println(flavor);
if (flavor.toString().indexOf("java.awt.Image")>0) {
Object o = cb.getData(flavor);
Image i = (Image)o;
// ImageIO will not write an Image
// It will write a BufferedImage (a type of RenderedImage)
BufferedImage bi = new BufferedImage(
i.getWidth(null),
i.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.drawImage(i, 0, 0, null);
g.dispose();
JScrollPane sp = new JScrollPane(new JLabel(new ImageIcon(bi)));
sp.setPreferredSize(new Dimension(800,600));
JOptionPane.showMessageDialog(null, sp);
File f = new File(
System.getProperty("user.home") +
File.separator +
"the.png");
ImageIO.write(bi, "png", f);
}
}
}
}