java - 在 Java 应用程序 (Swing) 中刷新/重新加载图像

标签 java image swing refresh repaint

我编写了一个程序,可以从网络摄像头(通过 OpenCV)拍摄快照并将其保存到项目中:

Colordetection
|__src
|    |
|   main.java
|
| Snapshot.png

现在我想刷新拍摄的快照,每次重新拍摄并重新绘制它时。

String imgText = "Snapshot.png";

JPanel panelScreenshot = new JPanel();
panelScreenshot.setBorder(new TitledBorder(null, "Screenshot", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panelScreenshot.setBounds(10, 11, 667, 543);
panelDisplay.add(panelScreenshot);
panelScreenshot.setLayout(null);

JLabel lblScreenshot = new JLabel();
lblScreenshot.setIcon(new ImageIcon(imgText));
lblScreenshot.setBounds(10, 21, 640, 480);
panelScreenshot.add(lblScreenshot);

JButton btnScreenshot = new JButton("Screenshot");
btnScreenshot.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {              


    VideoCapture cap = new VideoCapture(0);

    if(!cap.isOpened()){
      System.out.println("Webcam not Found");

      } 
      else 
      {
      System.out.println("Found Webcam:" + cap.toString());

      Mat frame = new Mat();
      cap.retrieve(frame);

      try {
          Thread.sleep(500);
          Highgui.imwrite("Snapshot.png", frame);

          lblScreenshot.repaint();                  

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

      }
      cap.release();                    
    }       
  }
});

问题是,它不会像我想要的那样重新绘制/刷新,只有当我重新启动程序时,新的屏幕截图才会出现。

我也尝试过:

ImageIcon icon = new ImageIcon(main.class.getResource("Snapshot.png"));

并更改了路径等,但没有帮助。

有人知道吗?

谢谢。

最佳答案

图像由 ImageIcon 缓存,因此您可以:

  1. 刷新图像,强制重新加载
  2. 使用ImageIO重新读取图像

简单的例子:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import java.net.*;

public class ImageReload extends JPanel implements ActionListener
{
    JLabel timeLabel;
    JLabel imageLabel;
    ImageIcon icon = new ImageIcon("timeLabel.jpg");

    public ImageReload()
    {
        setLayout( new BorderLayout() );

        timeLabel = new JLabel( new Date().toString() );
        imageLabel = new JLabel( timeLabel.getText() );

        add(timeLabel, BorderLayout.NORTH);
        add(imageLabel, BorderLayout.SOUTH);

        javax.swing.Timer timer = new javax.swing.Timer(1000, this);
        timer.start();
    }

    public void actionPerformed(ActionEvent e)
    {
        timeLabel.setText( new Date().toString() );

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    String imageName = "timeLabel.jpg";
                    BufferedImage image = ScreenImage.createImage(timeLabel);
                    ScreenImage.writeImage(image, imageName);

                    //  This works using ImageIO

//                  imageLabel.setIcon( new ImageIcon(ImageIO.read( new File(imageName) ) ) );

                    //  Or you can flush the image

                    ImageIcon icon = new ImageIcon(imageName);
                    icon.getImage().flush();
                    imageLabel.setIcon( icon );
                }
                catch(Exception e)
                {
                    System.out.println( e );
                }
            }
        });
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new ImageReload() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

此示例还需要 Screen Image动态重新生成图像的类。

关于java - 在 Java 应用程序 (Swing) 中刷新/重新加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28647127/

相关文章:

java - 错误用户输入Java的异常处理

java - 单击时获取 JButton 的名称

java - 为什么变量 n 在 Java 的第二个监听器中不可见?

html - 将可缩放图像定位在其他图像之上

java - 从一个 GUI 窗口跳转到另一个 GUI

java - 处理 POST 请求数据的有效方法

php - Laravel - 无法将存储在我的数据库中的 .png 图像作为 base64 编码字符串提供

javascript - 使用JQuery和PHP可以在浏览器上显示文本和图像吗?

java - 使用 SwingWorker 时应该如何处理异常?

java - 如何通过 ActionListener 类的 actionPerformed() 方法在 Frame 上绘制对象