initial
This commit is contained in:
50
src/main/java/io/swtc/proccessing/WebcamCaptureLoop.java
Normal file
50
src/main/java/io/swtc/proccessing/WebcamCaptureLoop.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package io.swtc.proccessing;
|
||||
|
||||
import com.github.sarxos.webcam.Webcam;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class WebcamCaptureLoop {
|
||||
private final Webcam webcam;
|
||||
private final Consumer<BufferedImage> onFrameCaptured;
|
||||
private volatile boolean running = false;
|
||||
|
||||
public WebcamCaptureLoop(Webcam webcam, Consumer<BufferedImage> onFrameCaptured) {
|
||||
this.webcam = webcam;
|
||||
this.onFrameCaptured = onFrameCaptured;
|
||||
|
||||
// Configure webcam
|
||||
Dimension[] sizes = webcam.getViewSizes();
|
||||
webcam.setViewSize(sizes[sizes.length - 1]);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (running) return;
|
||||
running = true;
|
||||
|
||||
Thread captureThread = new Thread(() -> {
|
||||
webcam.open();
|
||||
|
||||
while (running) {
|
||||
BufferedImage img = webcam.getImage();
|
||||
if (img != null) {
|
||||
onFrameCaptured.accept(img);
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(15);
|
||||
} catch (InterruptedException e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
webcam.close();
|
||||
});
|
||||
captureThread.setName("cam_cap_thread");
|
||||
captureThread.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user