samedi 21 février 2015

refreshing/reloading image in a Java Application (Swing)

I have made a programm to take a snapshot from the WebCam (via OpenCV) and safes it to the Project:





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



Now I would like to refresh the taken snapshot, everytime I re-take one and repaint it.





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();
}
}
});



The Problem is, it doesn't repaint/refresh as I want to, only if I restart the programm, the new screenshot is there.


I also tryied it with:





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


and changed the paths etc. but it didnt help.


does anyone has a clue?


thanks.


Aucun commentaire:

Enregistrer un commentaire