robot.createscreencapture返回的BuffereImage是否表示捕获屏幕过程之前或之后的状态?

fjaof16o  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(301)

当我调用robot.createscreencapture方法时,大约需要40毫秒才能返回一个BuffereImage。返回的bufferedimage是否表示40毫秒之前或之后的状态?
可能是中间状态?你知道我怎么能弄明白吗?

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();
        }
    }
}
kokeuurv

kokeuurv1#

我听从了戴维斯的建议。结果如下:

这意味着它捕获了一个中间状态。截图总共用了15毫秒来执行。它从一开始就在9毫秒后捕捉到了屏幕。返回缓冲后的图像又花了6毫秒。
下面是我用来测量它的代码:
millislock.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);
    }

}

截屏测试.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);

    }
}

相关问题