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>
70 lines
1.9 KiB
Java
70 lines
1.9 KiB
Java
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 volatile BufferedImage sourceImage;
|
|
private volatile BufferedImage processedImage;
|
|
|
|
private Function<BufferedImage, BufferedImage> imageProcessor;
|
|
private final AtomicBoolean repaintScheduled = new AtomicBoolean(false);
|
|
|
|
public CameraPanel() {
|
|
setBackground(Color.BLACK);
|
|
setPreferredSize(new Dimension(640, 480));
|
|
}
|
|
|
|
public void setImage(BufferedImage img) {
|
|
sourceImage = img;
|
|
|
|
if (repaintScheduled.compareAndSet(false, true)) {
|
|
SwingUtilities.invokeLater(() -> {
|
|
repaintScheduled.set(false);
|
|
repaint();
|
|
});
|
|
}
|
|
}
|
|
|
|
public void setImageProcessor(Function<BufferedImage, BufferedImage> processor) {
|
|
this.imageProcessor = processor;
|
|
}
|
|
|
|
@Override
|
|
protected void paintComponent(Graphics g) {
|
|
super.paintComponent(g);
|
|
|
|
BufferedImage src = sourceImage;
|
|
if (src == null) return;
|
|
|
|
BufferedImage img = src;
|
|
|
|
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;
|
|
}
|
|
}
|