【问题标题】:Does the bufferedImage returned by Robot.createScreenCapture represents the state before or after the process of capturing the screen?Robot.createScreenCapture返回的bufferedImage是代表截屏过程之前还是之后的状态?
【发布时间】:2020-11-18 21:56:32
【问题描述】:

当我调用方法 Robot.createScreenCapture 时,大约需要 40 毫秒才能返回一个 BufferedImage。返回的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();
        }
    }
}

【问题讨论】:

  • 在您正在捕获的屏幕部分显示与System.currentTimeMillis()同步的持续更新时钟。
  • 好主意。我会这样做并在此处发布结果。谢谢。

标签: java screen-capture awtrobot


【解决方案1】:

我听从了 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);
        
    }
}

【讨论】:

    猜你喜欢
    • 2012-11-16
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 2022-08-02
    • 1970-01-01
    相关资源
    最近更新 更多