Java:截图后获取照片点击效果

标签 java swing screenshot

我写了两个类,FullScreen ,其中包含屏幕截图的逻辑,以及 FullScreenGUI ,其中包含模拟照片点击效果的逻辑。

照片点击 效果基本上是在屏幕上短时间闪烁白色,比如截屏后 50 毫秒。它是由 JFrame 覆盖整个屏幕产生的不透明度为 1%。背景变白,然后不透明度从 1% 变为 100%,保持 50 毫秒,然后变回 1%。

FullScreen构造函数有两个参数,一个是截图的次数,另一个是中间的持续时间。

FullScreenGUI构造一个 JFrame ,最大化它,将背景设置为白色。当 fire()方法被调用时,它会根据需要更改不透明度以产生效果。

问题:

使用下面的代码,我可以在第一次截取屏幕截图时产生效果,但不能用于后续点击。假设,FullScreen使用参数 (4,2) 调用构造函数(即每次点击 4 次,持续时间为 2 秒),那么第一次点击会产生很好的效果,但其余 3 次点击则不会。 JFrameFullScreenGUI好像没有出现在前面,所以效果是看不出来的。我试过JFrame.setAlwaysOnTop(true) , JFrame.toFront()但他们似乎没有带JFrame到顶部。

截图没有问题,但是效果不行。您还有其他制作想法吗?

这是代码: FullScreen.java

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.Timer;
import javax.swing.filechooser.FileSystemView;

class FullScreen
{   
    int times, duration;
    Timer t;
    Robot r;
    BufferedImage bi;
    FullScreenGUI fg;

FullScreen(int tim, int duration) 
{
    fg = new FullScreenGUI();
    fg.setVisible(true);
    this.times = tim;
    this.duration = duration;
    try {
        r = new Robot();
    } catch (AWTException e) {
        e.printStackTrace();
    }
    System.out.println("Inside constructor");
    t = new Timer(duration*1000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {

            System.out.println("Inside action");
            if(times>0)
            {
                System.out.println("times: "+times);

                //Get the screenshot
                bi = capture();

                //Gives the path of the home directory. For windows, it'll go to your desktop
                FileSystemView filesys = FileSystemView.getFileSystemView();
                File fl = filesys.getHomeDirectory();
                saveImage(bi, fl);

                //Produces the "clicking" effect
                fg.setAlwaysOnTop(true);
                fg.toFront();
                fg.fire();

                times--;
            }
            else
                t.stop();
        }
    });
}

public void fire()
{
    t.start();
}

public void saveImage(BufferedImage source, File destination)
{
    System.out.println("Inside saveimage");
    if(destination.isDirectory())
    {
        System.out.println("destination: "+destination.getAbsolutePath());
        String tmp = destination.getAbsolutePath() + File.separator + "Screenshot";
        String str;
        int i=1;
        for(str=tmp; (new File(str+".png").exists()); i++)
        {
            str = tmp + "_" + String.valueOf(i);
            System.out.println("trying: "+str);
        }

        try {
            ImageIO.write(source, "png", new File(str+".png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public BufferedImage capture()
{
    System.out.println("Captured");
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    return r.createScreenCapture(new Rectangle(d));
}

public static void main(String arg[])
{
    //click 4 times each at an interval of 2 seconds
    FullScreen f = new FullScreen(4,2);

    f.fire();
    while(f.t.isRunning());
}
}

FullScreenGUI.java

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

class FullScreenGUI extends JFrame {

FullScreenGUI()
{
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setUndecorated(true);
    setResizable(false);
    setOpacity(0.01f);
    setAlwaysOnTop(true);

    setLayout(new BorderLayout());
    JLabel jl = new JLabel();
    jl.setBackground(Color.white);
    add(jl);        

    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void fire()
{
    System.out.println("click");
    setVisible(true);

    try{

    setOpacity(1f);
    Thread.sleep(50);
    setOpacity(0.1f);

    }catch(Exception e) { e.printStackTrace(); }

    setVisible(false);
}

}

最佳答案

问题的根源是Thread.sleep(50);

不要阻塞 EDT(事件调度线程)——当发生这种情况时,GUI 将“卡住”。而不是调用 Thread.sleep(n) 为这个任务实现一个 Swing Timer。参见 Concurrency in Swing更多细节。

关于Java:截图后获取照片点击效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14020308/

相关文章:

java - iReport 在 debian 上运行 java1.8

java super 没有按预期工作

macos - 如何在 Mac OS X 上拍摄忽略应用程序窗口的区域的屏幕截图

php - 如何在我的 Linux 服务器上创建 PDF 文件的缩略图/屏幕截图?

java - 将应用程序从 Eclipse 移植到 Android Studio 时出错

java - 在 Selenium Webdriver 中按 xPath 元素搜索

java - 如何显示过程调用的进度条

swing - 在 Scala TextArea 中设置字体和大小

java - JComponent 未显示在 JPanel 上

python - Selenium Webdriver,截图为 numpy 数组(Python)