我听从了 Tgdavies 的建议。结果如下:
这意味着它捕获了一个中间状态。屏幕截图总共执行了 15 毫秒。它从一开始就在 9 毫秒后捕获了屏幕。并且返回缓冲图像需要多花 6 毫秒。
这是我用来测量它的代码:
MillisClock.java:
import java.awt.Font;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class MillisClock{
private static JLabel lb = null;
public static void main(String[] args){
new Thread(new Runnable(){
@Override
public void run(){
while(true) {
try{
SwingUtilities.invokeAndWait(new Runnable(){
@Override
public void run(){
if(lb!=null) {
lb.setText("Current Millis: "+System.currentTimeMillis());
}
}
});
}
catch(InvocationTargetException e){
e.printStackTrace();
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
}).start();
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
createAndShowGui();
}
});
}
private static void createAndShowGui(){
JFrame frame = new JFrame("Millis Clock");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setAlwaysOnTop( true );
lb = new JLabel("Millis Here");
lb.setFont(new Font(Font.DIALOG, Font.BOLD, 22));
JPanel panel = new JPanel();
panel.add(lb);
frame.setContentPane(panel);
frame.setSize(400,100);
frame.setLocation(10,10);
frame.setVisible(true);
}
}
ScreenCaptureTest.java:
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class ScreenCaptureTest{
public static void main(String[] args) {
Robot robot = null;
try{
robot = new Robot();
long millisBefore = System.currentTimeMillis();
BufferedImage bi = robot.createScreenCapture(new Rectangle(0,0,500,120));
long millisAfter = System.currentTimeMillis();
long millisTaken = millisAfter - millisBefore;
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
createAndShowGui(millisBefore, millisAfter, millisTaken, bi);
}
});
}
catch(AWTException e){
e.printStackTrace();
}
}
private static void createAndShowGui(long millisBefore, long millisAfter, long millisTaken, BufferedImage bi){
JFrame frame = new JFrame("ScreenCapture Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTextArea textArea = new JTextArea(
"It took "+millisTaken+" milliseconds to execute.\n"
+ "MillisBefore = "+millisBefore+"\n"
+ "MillisAfter = "+millisAfter+"\n\n"
+ "The screen captured:\n\n"
);
textArea.setFont(new Font(Font.DIALOG, Font.PLAIN, 16));
textArea.setPreferredSize(new Dimension(500, 120));
textArea.setMargin(new Insets(10, 10, 10, 10));
JPanel panel = new JPanel(new BorderLayout(0, 0));
panel.add(textArea, BorderLayout.CENTER);
panel.add(new JLabel(new ImageIcon(bi)), BorderLayout.SOUTH);
frame.setContentPane(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}