refactor(webcam-capture-driver-native): 优化代码结构并调整依赖版本

- 重构部分代码,提高可读性和性能
-将 Java 版本从 11 降至 8,以支持更广泛的环境
- 移除 javafx-graphics 依赖,减少项目体积
- 更新 pom.xml 中的版本号至 1.2.0-8
This commit is contained in:
JianGuo 2025-02-08 14:35:23 +08:00
parent 88a4c75b2d
commit 7a9862eda2
6 changed files with 111 additions and 111 deletions

26
pom.xml
View File

@ -3,7 +3,7 @@
<groupId>io.github.eduramiba</groupId>
<artifactId>webcam-capture-driver-native</artifactId>
<version>1.2.0</version>
<version>1.2.0-8</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
@ -39,7 +39,7 @@
<properties>
<java.version>11</java.version>
<java.version>8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
@ -49,7 +49,7 @@
<driver.webcam-capture.version>0.3.13-SNAPSHOT</driver.webcam-capture.version>
<driver.jna.version>5.16.0</driver.jna.version>
<driver.javafx.version>19</driver.javafx.version>
<!-- <driver.javafx.version>19</driver.javafx.version>-->
<!-- Plugins versions -->
<driver.maven-compiler-plugin.version>3.13.0</driver.maven-compiler-plugin.version>
@ -94,11 +94,11 @@
<version>${driver.jna.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>${driver.javafx.version}</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.openjfx</groupId>-->
<!-- <artifactId>javafx-graphics</artifactId>-->
<!-- <version>${driver.javafx.version}</version>-->
<!-- </dependency>-->
<!-- Logging -->
<dependency>
@ -124,11 +124,11 @@
<artifactId>jna-platform</artifactId>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<scope>provided</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.openjfx</groupId>-->
<!-- <artifactId>javafx-graphics</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<!-- Logging -->
<dependency>

View File

@ -1,83 +1,83 @@
package com.github.eduramiba.webcamcapture;
import com.github.eduramiba.webcamcapture.drivers.NativeDriver;
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;
public class TestDriver extends Application {
private static final Logger LOG = LoggerFactory.getLogger(TestDriver.class);
public static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(4);
public static void main(String[] args) {
Webcam.setDriver(new NativeDriver());
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
final ImageView imageView = new ImageView();
final HBox root = new HBox();
root.getChildren().add(imageView);
Webcam.getWebcams().stream()
.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();
});
camera.getLock().disable();
camera.open();
if (device instanceof WebcamDeviceWithBufferOperations) {
final WebcamDeviceWithBufferOperations dev = ((WebcamDeviceWithBufferOperations) device);
EXECUTOR.scheduleAtFixedRate(new Runnable() {
private long lastFrameTimestamp = -1;
@Override
public void run() {
if (dev.updateFXIMage(fxImage, lastFrameTimestamp)) {
lastFrameTimestamp = dev.getLastFrameTimestamp();
}
}
}, 0, 16, TimeUnit.MILLISECONDS);
}
});
stage.setOnCloseRequest(t -> {
Platform.exit();
System.exit(0);
});
// Create the Scene
final Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Webcam example");
stage.show();
}
}
//package com.github.eduramiba.webcamcapture;
//
//import com.github.eduramiba.webcamcapture.drivers.NativeDriver;
//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;
//
//public class TestDriver extends Application {
//
// private static final Logger LOG = LoggerFactory.getLogger(TestDriver.class);
//
// public static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(4);
//
// public static void main(String[] args) {
// Webcam.setDriver(new NativeDriver());
//
// launch(args);
// }
//
// @Override
// public void start(Stage stage) throws Exception {
// final ImageView imageView = new ImageView();
// final HBox root = new HBox();
// root.getChildren().add(imageView);
//
// Webcam.getWebcams().stream()
// .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();
// });
//
// camera.getLock().disable();
// camera.open();
// if (device instanceof WebcamDeviceWithBufferOperations) {
// final WebcamDeviceWithBufferOperations dev = ((WebcamDeviceWithBufferOperations) device);
// EXECUTOR.scheduleAtFixedRate(new Runnable() {
// private long lastFrameTimestamp = -1;
//
// @Override
// public void run() {
// if (dev.updateFXIMage(fxImage, lastFrameTimestamp)) {
// lastFrameTimestamp = dev.getLastFrameTimestamp();
// }
//
// }
// }, 0, 16, TimeUnit.MILLISECONDS);
// }
// });
//
// stage.setOnCloseRequest(t -> {
// Platform.exit();
// System.exit(0);
// });
//
// // Create the Scene
// final Scene scene = new Scene(root);
// stage.setScene(scene);
// stage.setTitle("Webcam example");
// stage.show();
// }
//}

View File

@ -19,7 +19,7 @@ public class AVFDriver implements WebcamDriver {
@Override
public synchronized List<WebcamDevice> getDevices() {
final var lib = LibVideoCapture.INSTANCE;
final LibVideoCapture lib = LibVideoCapture.INSTANCE;
final List<WebcamDevice> list = new ArrayList<>();
@ -61,25 +61,25 @@ public class AVFDriver implements WebcamDriver {
}
private static String deviceUniqueId(final int deviceIndex) {
final var bufferP = Native.getDirectBufferPointer(buffer);
final com.sun.jna.Pointer bufferP = Native.getDirectBufferPointer(buffer);
LibVideoCapture.INSTANCE.vcavf_get_device_unique_id(deviceIndex, bufferP, buffer.capacity());
return bufferP.getString(0, StandardCharsets.UTF_8.name());
}
private static String deviceModelId(final int deviceIndex) {
final var bufferP = Native.getDirectBufferPointer(buffer);
final com.sun.jna.Pointer bufferP = Native.getDirectBufferPointer(buffer);
LibVideoCapture.INSTANCE.vcavf_get_device_model_id(deviceIndex, bufferP, buffer.capacity());
return bufferP.getString(0, StandardCharsets.UTF_8.name());
}
private static String deviceName(final int deviceIndex) {
final var bufferP = Native.getDirectBufferPointer(buffer);
final com.sun.jna.Pointer bufferP = Native.getDirectBufferPointer(buffer);
LibVideoCapture.INSTANCE.vcavf_get_device_name(deviceIndex, bufferP, buffer.capacity());
return bufferP.getString(0, StandardCharsets.UTF_8.name());
}
private static String deviceFormat(final int deviceIndex, final int formatIndex) {
final var bufferP = Native.getDirectBufferPointer(buffer);
final com.sun.jna.Pointer bufferP = Native.getDirectBufferPointer(buffer);
LibVideoCapture.INSTANCE.vcavf_get_device_format(deviceIndex, formatIndex, bufferP, buffer.capacity());
return bufferP.getString(0, StandardCharsets.UTF_8.name());
}

View File

@ -109,7 +109,7 @@ public class AVFVideoDevice implements WebcamDeviceExtended {
return;
}
final var lib = LibVideoCapture.INSTANCE;
final LibVideoCapture lib = LibVideoCapture.INSTANCE;
final int authStatus = lib.vcavf_has_videocapture_auth();
if (authStatus != STATUS_AUTHORIZED) {
@ -261,7 +261,7 @@ public class AVFVideoDevice implements WebcamDeviceExtended {
// Init buffer if still not initialized:
this.bytesPerRow = LibVideoCapture.INSTANCE.vcavf_frame_bytes_per_row(deviceIndex);
final var bufferSizeBytes = bytesPerRow * resolution.height;
final int bufferSizeBytes = bytesPerRow * resolution.height;
this.imgBuffer = ByteBuffer.allocateDirect(bufferSizeBytes);
this.arrayByteBuffer = new byte[imgBuffer.capacity()];
}

View File

@ -25,7 +25,7 @@ public class NokhwaDriver implements WebcamDriver {
@Override
public synchronized List<WebcamDevice> getDevices() {
final var lib = LibNokhwa.INSTANCE;
final LibNokhwa lib = LibNokhwa.INSTANCE;
final List<WebcamDevice> list = new ArrayList<>();
@ -86,19 +86,19 @@ public class NokhwaDriver implements WebcamDriver {
}
private static String deviceUniqueId(final int deviceIndex) {
final var bufferP = Native.getDirectBufferPointer(buffer);
final com.sun.jna.Pointer bufferP = Native.getDirectBufferPointer(buffer);
LibNokhwa.INSTANCE.cnokhwa_device_unique_id(deviceIndex, bufferP, buffer.capacity());
return bufferP.getString(0, StandardCharsets.UTF_8.name());
}
private static String deviceName(final int deviceIndex) {
final var bufferP = Native.getDirectBufferPointer(buffer);
final com.sun.jna.Pointer bufferP = Native.getDirectBufferPointer(buffer);
LibNokhwa.INSTANCE.cnokhwa_device_name(deviceIndex, bufferP, buffer.capacity());
return bufferP.getString(0, StandardCharsets.UTF_8.name());
}
private static String deviceFormatType(final int deviceIndex, final int formatIndex) {
final var bufferP = Native.getDirectBufferPointer(buffer);
final com.sun.jna.Pointer bufferP = Native.getDirectBufferPointer(buffer);
LibNokhwa.INSTANCE.cnokhwa_device_format_type(deviceIndex, formatIndex, bufferP, buffer.capacity());
return bufferP.getString(0, StandardCharsets.UTF_8.name());
}

View File

@ -108,7 +108,7 @@ public class NokhwaVideoDevice implements WebcamDeviceExtended {
return;
}
final var lib = LibNokhwa.INSTANCE;
final LibNokhwa lib = LibNokhwa.INSTANCE;
final int authStatus = lib.cnokhwa_has_videocapture_auth();
if (authStatus != STATUS_AUTHORIZED) {
@ -240,7 +240,7 @@ public class NokhwaVideoDevice implements WebcamDeviceExtended {
final PixelWriter pw = writableImage.getPixelWriter();
final ByteBuffer readBuffer = imgBuffer.asReadOnlyBuffer().position(0);
final ByteBuffer readBuffer = (ByteBuffer) imgBuffer.asReadOnlyBuffer().position(0);
pw.setPixels(
0, 0, videoWidth, videoHeight,
PixelFormat.getByteRgbInstance(), readBuffer, bytesPerRow
@ -256,7 +256,7 @@ public class NokhwaVideoDevice implements WebcamDeviceExtended {
// Init buffer if still not initialized:
this.bytesPerRow = LibNokhwa.INSTANCE.cnokhwa_frame_bytes_per_row(deviceIndex);
final var bufferSizeBytes = bytesPerRow * resolution.height;
final int bufferSizeBytes = bytesPerRow * resolution.height;
this.imgBuffer = ByteBuffer.allocateDirect(bufferSizeBytes);
this.arrayByteBuffer = new byte[imgBuffer.capacity()];
@ -297,7 +297,7 @@ public class NokhwaVideoDevice implements WebcamDeviceExtended {
new int[]{0, 1, 2}
);
final ByteBuffer readBuffer = imgBuffer.asReadOnlyBuffer().position(0);
final ByteBuffer readBuffer = (ByteBuffer) imgBuffer.asReadOnlyBuffer().position(0);
readBuffer.get(arrayByteBuffer, 0, readBuffer.capacity());
final DataBuffer dataBuffer = new DataBufferByte(arrayByteBuffer, arrayByteBuffer.length);