gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:57:19 25 4
gpt4 key购买 nike

我编写了一个程序,可以从网络摄像头(通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com