When I call the method Robot.createScreenCapture, it takes about 40 milliseconds to return a BufferedImage. Does the returned bufferedImage represents the state before or after the 40 milliseconds?
Maybe an intermediate state? Any idea how I can figure this out?
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
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,500));
long millisAfter = System.currentTimeMillis();
long millisTaken = millisAfter - millisBefore;
System.out.println("It took "+millisTaken+" milliseconds to execute.");
}
catch(AWTException e){
e.printStackTrace();
}
}
}
I followed the suggestion of Tgdavies. Here are the results:
It means that it captured an intermediate state. The screenshot took the total of 15 milliseconds to execute. It captured the screen after 9 milliseconds from the beginning. And it took 6 milliseconds more to return the buffered image.
Here are the code I used to measure it:
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.