JOptionPane for Errors, general refactoring, deprecated class removed (CameraRenderer) moved into Swing now

modified:
/ SwingCCTVManager ; Error Pane, refactoring
/ WebcamCaptureLoop ; cleanup method for closing cameras reliably, and some MessageDialogs for error handling (just fails lol)
/ SwingCameraWindow ; refactor some legacy code into a "modern" lambda function , Message Dialog for error handling , and some g2d stuff (paintComponent)

removed:
- CameraRenderer.java ; Deprecated Component, was used for SWT GL Surfaces. We dont do that now!

---

rattatwinko
This commit is contained in:
2026-01-13 17:33:47 +01:00
parent 370df03205
commit b767ba27b3
4 changed files with 50 additions and 119 deletions

View File

@@ -1,96 +0,0 @@
package io.swtc.proccessing;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.widgets.Composite;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class CameraRenderer {
private final GLCanvas canvas;
private int textureId = -1;
private ByteBuffer pixelBuffer;
private final AutoGainProcessor gainProcessor;
public CameraRenderer(Composite parent, org.eclipse.swt.opengl.GLData data) {
this.canvas = new GLCanvas(parent, 0, data);
this.gainProcessor = new AutoGainProcessor();
// Initialize OpenGL context immediately
this.canvas.setCurrent();
GL.createCapabilities();
initGL();
}
public GLCanvas getCanvas() {
return canvas;
}
private void initGL() {
GL11.glEnable(GL11.GL_TEXTURE_2D);
textureId = GL11.glGenTextures();
}
public void render(BufferedImage img) {
if (canvas.isDisposed()) return;
canvas.setCurrent();
int width = img.getWidth();
int height = img.getHeight();
int[] rgbArray = new int[width * height];
// this is hellishly unefficcient but who cares.
img.getRGB(0, 0, width, height, rgbArray, 0, width);
float[] gains = gainProcessor.calculateAutoGains(rgbArray);
int bufferSize = width * height * 3;
if (pixelBuffer == null || pixelBuffer.capacity() < bufferSize) {
pixelBuffer = ByteBuffer.allocateDirect(bufferSize);
pixelBuffer.order(ByteOrder.nativeOrder());
}
pixelBuffer.clear();
for (int pixel : rgbArray) {
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;
r = Math.min(255, (int)(r * gains[0]));
g = Math.min(255, (int)(g * gains[1]));
b = Math.min(255, (int)(b * gains[2]));
pixelBuffer.put((byte) r);
pixelBuffer.put((byte) g);
pixelBuffer.put((byte) b);
}
pixelBuffer.flip();
// this is just gl stuff
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glViewport(0, 0, canvas.getClientArea().width, canvas.getClientArea().height);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, width, height,
0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, pixelBuffer);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glColor3f(1.0f, 1.0f, 1.0f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex2f(-1, 1);
GL11.glTexCoord2f(1, 0); GL11.glVertex2f(1, 1);
GL11.glTexCoord2f(1, 1); GL11.glVertex2f(1, -1);
GL11.glTexCoord2f(0, 1); GL11.glVertex2f(-1, -1);
GL11.glEnd();
canvas.swapBuffers();
}
}

View File

@@ -1,9 +1,12 @@
package io.swtc.proccessing;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamException;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.util.function.Consumer;
import javax.swing.JOptionPane;
public class WebcamCaptureLoop {
private final Webcam webcam;
@@ -46,15 +49,39 @@ public class WebcamCaptureLoop {
}
try {
webcam.close();
} catch (IllegalStateException e) {
e.printStackTrace();
// show a error message from javax.swing.awt.JOptionPane
JOptionPane.showMessageDialog(
null,
"IllegalStateException@"+e,
"IllegalStateException",
JOptionPane.ERROR_MESSAGE
);
}
});
captureThread.setName("cam_cap_thread");
captureThread.start();
}
// method for cleaning up
private synchronized void cleanup() {
try {
// if the camera is open try to cloes it!
webcam.close();
} catch (WebcamException exception) {
JOptionPane.showMessageDialog(
null,
"Cleanup failed \nWebCamException@"+exception.getMessage(),
"WebCamException",
JOptionPane.ERROR_MESSAGE
);
}
}
public void stop() {
running = false;
cleanup();
}
}