some more optimizing!
now runs much better with lower power systems as we refactored code to firstly rule out double invokelaters, and we optimized the capture loop iteslf! and we introduced a ShowError class which will be refactored and used quite thoroughly! Signed-off-by: rattatwinko <seppmutterman@gmail.com>
This commit is contained in:
@@ -3,17 +3,19 @@ package io.swtc.proccessing;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Enhanced CameraPanel with support for custom image processors
|
||||
*/
|
||||
public class CameraPanel extends JPanel {
|
||||
private BufferedImage currentImage;
|
||||
private BufferedImage processedImage;
|
||||
|
||||
// Custom processor for advanced effects
|
||||
private volatile BufferedImage sourceImage;
|
||||
private volatile BufferedImage processedImage;
|
||||
|
||||
private Function<BufferedImage, BufferedImage> imageProcessor;
|
||||
private final AtomicBoolean repaintScheduled = new AtomicBoolean(false);
|
||||
|
||||
public CameraPanel() {
|
||||
setBackground(Color.BLACK);
|
||||
@@ -21,78 +23,47 @@ public class CameraPanel extends JPanel {
|
||||
}
|
||||
|
||||
public void setImage(BufferedImage img) {
|
||||
this.currentImage = img;
|
||||
processImage();
|
||||
repaint();
|
||||
sourceImage = img;
|
||||
|
||||
if (repaintScheduled.compareAndSet(false, true)) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
repaintScheduled.set(false);
|
||||
repaint();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void setImageProcessor(Function<BufferedImage, BufferedImage> processor) {
|
||||
this.imageProcessor = processor;
|
||||
processImage();
|
||||
repaint();
|
||||
}
|
||||
|
||||
private void processImage() {
|
||||
if (currentImage == null) {
|
||||
processedImage = null;
|
||||
return;
|
||||
}
|
||||
|
||||
BufferedImage result = currentImage;
|
||||
|
||||
// Apply basic transforms first
|
||||
result = applyBasicEffects(result);
|
||||
|
||||
// Apply custom processor if set
|
||||
if (imageProcessor != null) {
|
||||
result = imageProcessor.apply(result);
|
||||
}
|
||||
|
||||
processedImage = result;
|
||||
}
|
||||
|
||||
private BufferedImage applyBasicEffects(BufferedImage img) {
|
||||
int width = img.getWidth();
|
||||
int height = img.getHeight();
|
||||
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
// Handle mirror/flip/rotate
|
||||
int srcX = x;
|
||||
int srcY = y;
|
||||
|
||||
// Ensure coordinates are in bounds
|
||||
srcX = Math.max(0, Math.min(width - 1, srcX));
|
||||
srcY = Math.max(0, Math.min(height - 1, srcY));
|
||||
|
||||
int rgb = img.getRGB(srcX, srcY);
|
||||
int r = (rgb >> 16) & 0xFF;
|
||||
int g = (rgb >> 8) & 0xFF;
|
||||
int b = rgb & 0xFF;
|
||||
|
||||
result.setRGB(x, y, (r << 16) | (g << 8) | b);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
if (processedImage != null) {
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
BufferedImage src = sourceImage;
|
||||
if (src == null) return;
|
||||
|
||||
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
BufferedImage img = src;
|
||||
|
||||
g2d.drawImage(processedImage, 0, 0, getWidth(), getHeight(), null);
|
||||
if (imageProcessor != null) {
|
||||
// we only apply the proccessor if it is present
|
||||
// that is due too CPU Usage ; This project was built for a DUAL Core CPU, not a quad or even 6 Core CPU
|
||||
img = imageProcessor.apply(src);
|
||||
}
|
||||
|
||||
processedImage = img;
|
||||
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.setRenderingHint(
|
||||
RenderingHints.KEY_INTERPOLATION,
|
||||
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR
|
||||
);
|
||||
|
||||
g2d.drawImage(img, 0, 0, getWidth(), getHeight(), null);
|
||||
}
|
||||
|
||||
public BufferedImage getCurrentProcessedImage() {
|
||||
return processedImage;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user