a new windowing system!

add:
+ CameraPanel ; To make life easier for coders
+ SwingIFrame ; Which is now our main UI component
+ VideoRecorder ; A helper Class for SwingIFrame to record cameras!

changed:
/ SwingCCTVManager ; changes for the new UI Component

deprecation:
/-/ AutoGainProcessor ; cause it isnt needed anymore, back then this was needed cause we opened the webcams manually (color wise)

---
rattatwinko
This commit is contained in:
2026-01-13 20:52:32 +01:00
parent b767ba27b3
commit ccc3d264f7
5 changed files with 698 additions and 27 deletions

View File

@@ -0,0 +1,311 @@
package io.swtc;
import com.github.sarxos.webcam.Webcam;
import io.swtc.proccessing.WebcamCaptureLoop;
import io.swtc.proccessing.CameraPanel;
import io.swtc.recording.VideoRecorder;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
*
* This file is basically just UI, its boring the interesting stuff is in the utilities section!
*
* */
public class SwingIFrame {
private final JFrame mainFrame;
private final JDesktopPane desktopPane;
public SwingIFrame() {
mainFrame = new JFrame("viewer");
mainFrame.setSize(1280,720);
mainFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
desktopPane = new JDesktopPane();
desktopPane.setBackground(Color.WHITE);
mainFrame.add(desktopPane, BorderLayout.CENTER);
}
public void addCameraInternalFrame(Webcam webcam) {
JInternalFrame iframe = new JInternalFrame(
webcam.getName(),
true, true, true, true
);
CameraPanel cameraPanel = new CameraPanel();
WebcamCaptureLoop captureLoop = new WebcamCaptureLoop(webcam, (BufferedImage img) ->
SwingUtilities.invokeLater(() -> cameraPanel.setImage(img))
);
JPanel contentPanel = new JPanel(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("View", cameraPanel);
tabbedPane.addTab("Capture", createCapturePanel(cameraPanel, webcam));
tabbedPane.addTab("Effects", createEffectsPanel(cameraPanel));
contentPanel.add(tabbedPane, BorderLayout.CENTER);
iframe.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter() {
@Override
public void internalFrameClosing(javax.swing.event.InternalFrameEvent e) {
// if we dont call this the camera stays open until the procces dies.
captureLoop.stop();
}
});
iframe.add(contentPanel);
iframe.setSize(600, 500);
int offset = desktopPane.getAllFrames().length * 30;
iframe.setLocation(offset, offset);
desktopPane.add(iframe);
iframe.setVisible(true);
captureLoop.start();
}
private JPanel createCapturePanel(CameraPanel cameraPanel, Webcam webcam) {
JPanel panel = new JPanel(new BorderLayout(10, 10));
panel.setBorder(new EmptyBorder(15, 15, 15, 15));
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
// Screenshot section
JPanel screenshotPanel = new JPanel();
screenshotPanel.setLayout(new BoxLayout(screenshotPanel, BoxLayout.Y_AXIS));
screenshotPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Screenshot",
TitledBorder.LEFT, TitledBorder.TOP));
JPanel screenshotPathPanel = new JPanel(new BorderLayout(5, 5));
JTextField screenshotPath = new JTextField(System.getProperty("user.home") + File.separator + "screenshots");
JButton browseSS = new JButton("...");
browseSS.setPreferredSize(new Dimension(40, 25));
browseSS.addActionListener(e -> browseDirectory(screenshotPath, panel));
screenshotPathPanel.add(screenshotPath, BorderLayout.CENTER);
screenshotPathPanel.add(browseSS, BorderLayout.EAST);
JButton takeScreenshot = new JButton("Take Screenshot (S)");
takeScreenshot.setAlignmentX(Component.CENTER_ALIGNMENT);
takeScreenshot.addActionListener(e -> saveSnapshot(cameraPanel, webcam, screenshotPath.getText(), panel));
screenshotPanel.add(screenshotPathPanel);
screenshotPanel.add(Box.createRigidArea(new Dimension(0, 10)));
screenshotPanel.add(takeScreenshot);
// Recording section
JPanel recordPanel = new JPanel();
recordPanel.setLayout(new BoxLayout(recordPanel, BoxLayout.Y_AXIS));
recordPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Recording",
TitledBorder.LEFT, TitledBorder.TOP));
JPanel recordPathPanel = new JPanel(new BorderLayout(5, 5));
JTextField recordPath = new JTextField(System.getProperty("user.home") + File.separator + "recordings");
JButton browseRec = new JButton("...");
browseRec.setPreferredSize(new Dimension(40, 25));
browseRec.addActionListener(e -> browseDirectory(recordPath, panel));
recordPathPanel.add(recordPath, BorderLayout.CENTER);
recordPathPanel.add(browseRec, BorderLayout.EAST);
VideoRecorder recorder = new VideoRecorder();
JButton recordButton = new JButton("Start Recording (R)");
JLabel recordingStatus = new JLabel("Ready");
recordingStatus.setAlignmentX(Component.CENTER_ALIGNMENT);
recordButton.setAlignmentX(Component.CENTER_ALIGNMENT);
recordButton.addActionListener(e -> {
if (!recorder.isRecording()) {
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String filename = "recording_" + webcam.getName().replaceAll("[^a-zA-Z0-9]", "_")
+ "_" + timestamp + ".mp4";
File dir = new File(recordPath.getText());
dir.mkdirs();
try {
recorder.startRecording(cameraPanel, new File(dir, filename));
recordButton.setText("Stop Recording");
recordingStatus.setText("Recording...");
recordingStatus.setForeground(Color.RED);
} catch (Exception ex) {
JOptionPane.showMessageDialog(panel,
"Error starting recording: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
} else {
try {
File saved = recorder.stopRecording();
recordButton.setText("Start Recording (R)");
recordingStatus.setText("Saved: " + saved.getName());
recordingStatus.setForeground(Color.BLACK);
} catch (Exception ex) {
recordButton.setText("Start Recording (R)");
recordingStatus.setText("Error saving");
recordingStatus.setForeground(Color.RED);
JOptionPane.showMessageDialog(panel,
"Error saving recording: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
});
recordPanel.add(recordPathPanel);
recordPanel.add(Box.createRigidArea(new Dimension(0, 10)));
recordPanel.add(recordButton);
recordPanel.add(Box.createRigidArea(new Dimension(0, 5)));
recordPanel.add(recordingStatus);
mainPanel.add(screenshotPanel);
mainPanel.add(Box.createRigidArea(new Dimension(0, 15)));
mainPanel.add(recordPanel);
mainPanel.add(Box.createVerticalGlue());
panel.add(mainPanel, BorderLayout.NORTH);
return panel;
}
private JPanel createEffectsPanel(CameraPanel cameraPanel) {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(new EmptyBorder(15, 15, 15, 15));
JPanel transformPanel = new JPanel(new GridLayout(0, 1, 5, 5));
transformPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Transform",
TitledBorder.LEFT, TitledBorder.TOP));
JCheckBox mirrorCheck = new JCheckBox("Mirror Horizontal");
JCheckBox flipCheck = new JCheckBox("Flip Vertical");
JCheckBox rotateCheck = new JCheckBox("Rotate 180°");
mirrorCheck.addActionListener(e -> cameraPanel.setMirror(mirrorCheck.isSelected()));
flipCheck.addActionListener(e -> cameraPanel.setFlip(flipCheck.isSelected()));
rotateCheck.addActionListener(e -> cameraPanel.setRotate(rotateCheck.isSelected()));
transformPanel.add(mirrorCheck);
transformPanel.add(flipCheck);
transformPanel.add(rotateCheck);
JPanel colorPanel = new JPanel();
colorPanel.setLayout(new BoxLayout(colorPanel, BoxLayout.Y_AXIS));
colorPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Color",
TitledBorder.LEFT, TitledBorder.TOP));
JCheckBox grayscaleCheck = new JCheckBox("Grayscale");
JCheckBox invertCheck = new JCheckBox("Invert Colors");
grayscaleCheck.addActionListener(e -> cameraPanel.setGrayscale(grayscaleCheck.isSelected()));
invertCheck.addActionListener(e -> cameraPanel.setInvert(invertCheck.isSelected()));
colorPanel.add(grayscaleCheck);
colorPanel.add(Box.createRigidArea(new Dimension(0, 5)));
colorPanel.add(invertCheck);
JPanel brightnessPanel = new JPanel();
brightnessPanel.setLayout(new BoxLayout(brightnessPanel, BoxLayout.Y_AXIS));
brightnessPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Adjustments",
TitledBorder.LEFT, TitledBorder.TOP));
JPanel brightnessSliderPanel = new JPanel(new BorderLayout());
JLabel brightnessLabel = new JLabel("Brightness: 0");
JSlider brightnessSlider = new JSlider(-100, 100, 0);
brightnessSlider.addChangeListener(e -> {
int value = brightnessSlider.getValue();
brightnessLabel.setText("Brightness: " + value);
cameraPanel.setBrightness(value);
});
brightnessSliderPanel.add(brightnessLabel, BorderLayout.NORTH);
brightnessSliderPanel.add(brightnessSlider, BorderLayout.CENTER);
JPanel contrastSliderPanel = new JPanel(new BorderLayout());
JLabel contrastLabel = new JLabel("Contrast: 1.0");
JSlider contrastSlider = new JSlider(0, 200, 100);
contrastSlider.addChangeListener(e -> {
float value = contrastSlider.getValue() / 100f;
contrastLabel.setText("Contrast: " + String.format("%.1f", value));
cameraPanel.setContrast(value);
});
contrastSliderPanel.add(contrastLabel, BorderLayout.NORTH);
contrastSliderPanel.add(contrastSlider, BorderLayout.CENTER);
brightnessPanel.add(brightnessSliderPanel);
brightnessPanel.add(Box.createRigidArea(new Dimension(0, 10)));
brightnessPanel.add(contrastSliderPanel);
JButton resetButton = new JButton("Reset All Effects");
resetButton.setAlignmentX(Component.CENTER_ALIGNMENT);
resetButton.addActionListener(e -> {
mirrorCheck.setSelected(false);
flipCheck.setSelected(false);
rotateCheck.setSelected(false);
grayscaleCheck.setSelected(false);
invertCheck.setSelected(false);
brightnessSlider.setValue(0);
contrastSlider.setValue(100);
cameraPanel.resetEffects();
});
panel.add(transformPanel);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(colorPanel);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(brightnessPanel);
panel.add(Box.createRigidArea(new Dimension(0, 15)));
panel.add(resetButton);
panel.add(Box.createVerticalGlue());
return panel;
}
private void browseDirectory(JTextField field, Component parent) {
JFileChooser chooser = new JFileChooser(field.getText());
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
field.setText(chooser.getSelectedFile().getAbsolutePath());
}
}
private void saveSnapshot(CameraPanel cameraPanel, Webcam webcam, String directory, Component parent) {
BufferedImage img = cameraPanel.getCurrentProcessedImage();
if (img != null) {
try {
File dir = new File(directory);
dir.mkdirs();
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String filename = "screenshot_" + webcam.getName().replaceAll("[^a-zA-Z0-9]", "_")
+ "_" + timestamp + ".png";
File file = new File(dir, filename);
ImageIO.write(img, "PNG", file);
} catch (Exception ex) {
JOptionPane.showMessageDialog(parent,
"Error: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
public void show() {
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
SwingIFrame dashboard = new SwingIFrame();
dashboard.show();
});
}
}