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

时间:2015-02-21 14:53:01

标签: java image swing refresh repaint

我制作了一个程序来从WebCam拍摄快照(通过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"));

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

有没有人有线索?

感谢。

2 个答案:

答案 0 :(得分:1)

永远不要在EDT上拨打Thread.sleep。这会冻结整个用户界面。 Swing是单线程的,所以如果你让那个线程休眠,整个UI就会被冻结

话虽如此,原因是图标只加载一次。磁盘上的实际映像发生变化并不重要。你需要打电话

lblScreenshot.setIcon(new ImageIcon(imgText));
更换光盘上的图像后再次

。这将重新加载图像。

答案 1 :(得分:1)

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

  1. 刷新图像,强制重新加载
  2. 使用ImageIO重新读取图像
  3. 简单示例:

    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类动态重新生成图像。