屏幕截图问题 - 不包括自己的应用程序.从截图 [英] ScreenShot issue - excluding own app. from screenshot

查看:59
本文介绍了屏幕截图问题 - 不包括自己的应用程序.从截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,我真的不知道如何解决,基本上我想对我的桌面进行截图(使用Robot"类),不包括实际的程序 GUI 组件.最初我认为可以通过暂时隐藏组件来完成,但每次制作新屏幕截图时,组件都会包含在图像中.

I have a small issue that I don’t really know how to solve, basically I want to take a screenshot (using the "Robot" class) of my desktop excluding the actual program GUI components. Originally I believed it can be done by temporary hiding the components but every time a new screenshot is made the components are included in the image.

这是用于获取屏幕截图的按钮的 actionPerformed 方法中包含的块:

This is the block included in the actionPerformed method for the button that takes the screenshot:

if (command.equals("zoom")) {
    setComponentVisability(false);//try to hide the components from the robot
    zt.screenShoot();//take the screenshot
    setComponentVisability(true);//show the components
} 

"zt.screenShoot" 截取屏幕截图并将其返回到一个新的 JFrame(用于调试)中,在我的主框架中我正在使用

"zt.screenShoot" takes the screenshot and returns it in a new JFrame (for debugging),in my main frame I am using the

com.sun.awt.AWTUtilities.setWindowOpaque(systemWindow, false);

使背景透明的方法;不确定这是否与此问题有关.

method to make the background transparent; not sure if that has anything to do with this issue.

任何帮助都会很棒,谢谢

any help would be great, thanks

推荐答案

在隐藏组件和截取屏幕截图之间使用短暂的延迟.

Use a short delay between hiding the component and taking the screenshot.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class TestScreenshot {

    static JLabel screenshot;

    public static void main(String[] args) throws Exception {
        final Robot robot = new Robot();
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                final JFrame f = new JFrame("Screenshot");

                JPanel gui = new JPanel(new BorderLayout(3,3));

                gui.setBorder(new EmptyBorder(5,5,5,5));

                f.setContentPane(gui);

                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                screenshot = new JLabel();
                JScrollPane scroll = new JScrollPane(screenshot);
                scroll.setPreferredSize(new Dimension(800,600));
                gui.add(scroll, BorderLayout.CENTER);

                final ActionListener al = new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        Rectangle screenSize = new Rectangle(
                            Toolkit.getDefaultToolkit().getScreenSize());
                        Image image = robot.createScreenCapture(screenSize);
                        setImage(image);
                        f.setVisible(true);
                    }
                };

                JButton grabScreen = new JButton("Grab Screen");
                grabScreen.addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        f.setVisible(false);
                        Timer timer = new Timer(400, al);
                        timer.setRepeats(false);
                        timer.start();
                    }
                } );
                gui.add(grabScreen, BorderLayout.SOUTH);

                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }

    public static void setImage(Image image) {
        screenshot.setIcon(new ImageIcon(image));
    }
}

这篇关于屏幕截图问题 - 不包括自己的应用程序.从截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆