webcam-capture-driver-native/README.md

107 lines
4.4 KiB
Markdown
Raw Normal View History

2021-06-17 16:35:42 +08:00
# Introduction
This is a native driver for [Webcam Capture](https://github.com/sarxos/webcam-capture) that is reliable, has very good performance, fast startup time and is able to correctly list the detailed capabilities of video devices such as resolutions and device IDs.
2022-12-09 20:32:44 +08:00
Currently it works on Windows and Mac.
For Windows, it uses the `CaptureManagerDriver`, based on [CaptureManager-SDK](https://www.codeproject.com/Articles/1017223/CaptureManager-SDK-Capturing-Recording-and-Streami), which uses the MediaFoundation Windows API.
For Mac, it uses `AVFDriver`, based on a [custom library](https://github.com/eduramiba/libvideocapture-avfoundation) that uses [AVFoundation](https://developer.apple.com/av-foundation/).
2021-06-17 16:35:42 +08:00
# How to use
1. Download this repository and run `mvn install`
2. Add `com.github.eduramiba:webcam-capture-driver-native:1.0.0-SNAPSHOT` dependency to your application.
2022-12-09 20:32:44 +08:00
3. Use the driver with `Webcam.setDriver(new NativeDriver())`
4. List the devices with `Webcam.getWebcams()` as normal and use the library in your preferred way. In JavaFX it's recommended to do it as in the example below.
2021-06-17 16:35:42 +08:00
# Simple example with JavaFX
```java
2022-12-09 20:32:44 +08:00
import com.github.eduramiba.webcamcapture.drivers.NativeDriver;
2021-06-17 16:35:42 +08:00
import com.github.eduramiba.webcamcapture.drivers.WebcamDeviceWithBufferOperations;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamDevice;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2022-12-09 20:32:44 +08:00
public class TestDriver extends Application {
private static final Logger LOG = LoggerFactory.getLogger(TestDriver.class);
2021-06-17 16:35:42 +08:00
public static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(4);
public static void main(String[] args) {
2022-12-09 20:32:44 +08:00
Webcam.setDriver(new NativeDriver());
2021-06-17 16:35:42 +08:00
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
2022-12-09 20:32:44 +08:00
final ImageView imageView = new ImageView();
final HBox root = new HBox();
2021-06-17 16:35:42 +08:00
root.getChildren().add(imageView);
Webcam.getWebcams().stream()
2022-12-09 20:32:44 +08:00
.findFirst()
.ifPresent((final Webcam camera) -> {
final WebcamDevice device = camera.getDevice();
LOG.info("Found camera: {}, device = {}", camera, device);
final int width = device.getResolution().width;
final int height = device.getResolution().height;
final WritableImage fxImage = new WritableImage(width, height);
Platform.runLater(() -> {
imageView.setImage(fxImage);
stage.setWidth(width);
stage.setHeight(height);
stage.centerOnScreen();
2021-06-17 16:35:42 +08:00
});
2022-12-09 20:32:44 +08:00
camera.getLock().disable();
camera.open();
if (device instanceof WebcamDeviceWithBufferOperations) {
EXECUTOR.scheduleAtFixedRate(() -> {
((WebcamDeviceWithBufferOperations) device).updateFXIMage(fxImage);
}, 0, 16, TimeUnit.MILLISECONDS);
}
});
stage.setOnCloseRequest(t -> {
Platform.exit();
System.exit(0);
});
2021-06-17 16:35:42 +08:00
// Create the Scene
2022-12-09 20:32:44 +08:00
final Scene scene = new Scene(root);
2021-06-17 16:35:42 +08:00
stage.setScene(scene);
2022-12-09 20:32:44 +08:00
stage.setTitle("Webcam example");
2021-06-17 16:35:42 +08:00
stage.show();
}
}
2022-12-09 20:32:44 +08:00
2021-06-17 16:35:42 +08:00
```
# Future work
* Publish this as a maven central artifact. At the moment you will need to build it yourself.
2022-12-09 20:32:44 +08:00
* Implement Linux driver
2021-06-17 16:35:42 +08:00
# Notes
2022-12-09 20:32:44 +08:00
The source code in `natives` folder and `capturemanager` java package has been copied from [CaptureManager-SDK](https://www.codeproject.com/Articles/1017223/CaptureManager-SDK-Capturing-Recording-and-Streami) and slightly improved for this driver. This code is not idiomatic java and needs improvement.
The DLLs for Windows can just be copied along with your program.
The native dynamic libraries for Mac are on `src/main/resources` and loaded by JNA from inside the JAR.
Note that if you want to distribute a Mac app you will need to properly codesign the dylib files with entitlements, have an Info.plist, notarization...