refactor(webcam-capture-driver-native): 优化代码结构并调整依赖版本
- 重构部分代码,提高可读性和性能 -将 Java 版本从 11 降至 8,以支持更广泛的环境 - 移除 javafx-graphics 依赖,减少项目体积 - 更新 pom.xml 中的版本号至 1.2.0-8
This commit is contained in:
parent
88a4c75b2d
commit
7a9862eda2
26
pom.xml
26
pom.xml
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
<groupId>io.github.eduramiba</groupId>
|
<groupId>io.github.eduramiba</groupId>
|
||||||
<artifactId>webcam-capture-driver-native</artifactId>
|
<artifactId>webcam-capture-driver-native</artifactId>
|
||||||
<version>1.2.0</version>
|
<version>1.2.0-8</version>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@
|
||||||
|
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>11</java.version>
|
<java.version>8</java.version>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<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.webcam-capture.version>0.3.13-SNAPSHOT</driver.webcam-capture.version>
|
||||||
<driver.jna.version>5.16.0</driver.jna.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 -->
|
<!-- Plugins versions -->
|
||||||
<driver.maven-compiler-plugin.version>3.13.0</driver.maven-compiler-plugin.version>
|
<driver.maven-compiler-plugin.version>3.13.0</driver.maven-compiler-plugin.version>
|
||||||
|
@ -94,11 +94,11 @@
|
||||||
<version>${driver.jna.version}</version>
|
<version>${driver.jna.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<!-- <dependency>-->
|
||||||
<groupId>org.openjfx</groupId>
|
<!-- <groupId>org.openjfx</groupId>-->
|
||||||
<artifactId>javafx-graphics</artifactId>
|
<!-- <artifactId>javafx-graphics</artifactId>-->
|
||||||
<version>${driver.javafx.version}</version>
|
<!-- <version>${driver.javafx.version}</version>-->
|
||||||
</dependency>
|
<!-- </dependency>-->
|
||||||
|
|
||||||
<!-- Logging -->
|
<!-- Logging -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -124,11 +124,11 @@
|
||||||
<artifactId>jna-platform</artifactId>
|
<artifactId>jna-platform</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<!-- <dependency>-->
|
||||||
<groupId>org.openjfx</groupId>
|
<!-- <groupId>org.openjfx</groupId>-->
|
||||||
<artifactId>javafx-graphics</artifactId>
|
<!-- <artifactId>javafx-graphics</artifactId>-->
|
||||||
<scope>provided</scope>
|
<!-- <scope>provided</scope>-->
|
||||||
</dependency>
|
<!-- </dependency>-->
|
||||||
|
|
||||||
<!-- Logging -->
|
<!-- Logging -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -1,83 +1,83 @@
|
||||||
package com.github.eduramiba.webcamcapture;
|
//package com.github.eduramiba.webcamcapture;
|
||||||
|
//
|
||||||
import com.github.eduramiba.webcamcapture.drivers.NativeDriver;
|
//import com.github.eduramiba.webcamcapture.drivers.NativeDriver;
|
||||||
import com.github.eduramiba.webcamcapture.drivers.WebcamDeviceWithBufferOperations;
|
//import com.github.eduramiba.webcamcapture.drivers.WebcamDeviceWithBufferOperations;
|
||||||
import com.github.sarxos.webcam.Webcam;
|
//import com.github.sarxos.webcam.Webcam;
|
||||||
import com.github.sarxos.webcam.WebcamDevice;
|
//import com.github.sarxos.webcam.WebcamDevice;
|
||||||
import java.util.concurrent.Executors;
|
//import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
//import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
//import java.util.concurrent.TimeUnit;
|
||||||
import javafx.application.Application;
|
//import javafx.application.Application;
|
||||||
import javafx.application.Platform;
|
//import javafx.application.Platform;
|
||||||
import javafx.scene.Scene;
|
//import javafx.scene.Scene;
|
||||||
import javafx.scene.image.ImageView;
|
//import javafx.scene.image.ImageView;
|
||||||
import javafx.scene.image.WritableImage;
|
//import javafx.scene.image.WritableImage;
|
||||||
import javafx.scene.layout.HBox;
|
//import javafx.scene.layout.HBox;
|
||||||
import javafx.stage.Stage;
|
//import javafx.stage.Stage;
|
||||||
import org.slf4j.Logger;
|
//import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
//import org.slf4j.LoggerFactory;
|
||||||
|
//
|
||||||
public class TestDriver extends Application {
|
//public class TestDriver extends Application {
|
||||||
|
//
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(TestDriver.class);
|
// private static final Logger LOG = LoggerFactory.getLogger(TestDriver.class);
|
||||||
|
//
|
||||||
public static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(4);
|
// public static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(4);
|
||||||
|
//
|
||||||
public static void main(String[] args) {
|
// public static void main(String[] args) {
|
||||||
Webcam.setDriver(new NativeDriver());
|
// Webcam.setDriver(new NativeDriver());
|
||||||
|
//
|
||||||
launch(args);
|
// launch(args);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public void start(Stage stage) throws Exception {
|
// public void start(Stage stage) throws Exception {
|
||||||
final ImageView imageView = new ImageView();
|
// final ImageView imageView = new ImageView();
|
||||||
final HBox root = new HBox();
|
// final HBox root = new HBox();
|
||||||
root.getChildren().add(imageView);
|
// root.getChildren().add(imageView);
|
||||||
|
//
|
||||||
Webcam.getWebcams().stream()
|
// Webcam.getWebcams().stream()
|
||||||
.findFirst()
|
// .findFirst()
|
||||||
.ifPresent((final Webcam camera) -> {
|
// .ifPresent((final Webcam camera) -> {
|
||||||
final WebcamDevice device = camera.getDevice();
|
// final WebcamDevice device = camera.getDevice();
|
||||||
LOG.info("Found camera: {}, device = {}", camera, device);
|
// LOG.info("Found camera: {}, device = {}", camera, device);
|
||||||
|
//
|
||||||
final int width = device.getResolution().width;
|
// final int width = device.getResolution().width;
|
||||||
final int height = device.getResolution().height;
|
// final int height = device.getResolution().height;
|
||||||
final WritableImage fxImage = new WritableImage(width, height);
|
// final WritableImage fxImage = new WritableImage(width, height);
|
||||||
Platform.runLater(() -> {
|
// Platform.runLater(() -> {
|
||||||
imageView.setImage(fxImage);
|
// imageView.setImage(fxImage);
|
||||||
stage.setWidth(width);
|
// stage.setWidth(width);
|
||||||
stage.setHeight(height);
|
// stage.setHeight(height);
|
||||||
stage.centerOnScreen();
|
// stage.centerOnScreen();
|
||||||
});
|
// });
|
||||||
|
//
|
||||||
camera.getLock().disable();
|
// camera.getLock().disable();
|
||||||
camera.open();
|
// camera.open();
|
||||||
if (device instanceof WebcamDeviceWithBufferOperations) {
|
// if (device instanceof WebcamDeviceWithBufferOperations) {
|
||||||
final WebcamDeviceWithBufferOperations dev = ((WebcamDeviceWithBufferOperations) device);
|
// final WebcamDeviceWithBufferOperations dev = ((WebcamDeviceWithBufferOperations) device);
|
||||||
EXECUTOR.scheduleAtFixedRate(new Runnable() {
|
// EXECUTOR.scheduleAtFixedRate(new Runnable() {
|
||||||
private long lastFrameTimestamp = -1;
|
// private long lastFrameTimestamp = -1;
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public void run() {
|
// public void run() {
|
||||||
if (dev.updateFXIMage(fxImage, lastFrameTimestamp)) {
|
// if (dev.updateFXIMage(fxImage, lastFrameTimestamp)) {
|
||||||
lastFrameTimestamp = dev.getLastFrameTimestamp();
|
// lastFrameTimestamp = dev.getLastFrameTimestamp();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
}, 0, 16, TimeUnit.MILLISECONDS);
|
// }, 0, 16, TimeUnit.MILLISECONDS);
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
//
|
||||||
stage.setOnCloseRequest(t -> {
|
// stage.setOnCloseRequest(t -> {
|
||||||
Platform.exit();
|
// Platform.exit();
|
||||||
System.exit(0);
|
// System.exit(0);
|
||||||
});
|
// });
|
||||||
|
//
|
||||||
// Create the Scene
|
// // Create the Scene
|
||||||
final Scene scene = new Scene(root);
|
// final Scene scene = new Scene(root);
|
||||||
stage.setScene(scene);
|
// stage.setScene(scene);
|
||||||
stage.setTitle("Webcam example");
|
// stage.setTitle("Webcam example");
|
||||||
stage.show();
|
// stage.show();
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
|
@ -19,7 +19,7 @@ public class AVFDriver implements WebcamDriver {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized List<WebcamDevice> getDevices() {
|
public synchronized List<WebcamDevice> getDevices() {
|
||||||
final var lib = LibVideoCapture.INSTANCE;
|
final LibVideoCapture lib = LibVideoCapture.INSTANCE;
|
||||||
|
|
||||||
final List<WebcamDevice> list = new ArrayList<>();
|
final List<WebcamDevice> list = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -61,25 +61,25 @@ public class AVFDriver implements WebcamDriver {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String deviceUniqueId(final int deviceIndex) {
|
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());
|
LibVideoCapture.INSTANCE.vcavf_get_device_unique_id(deviceIndex, bufferP, buffer.capacity());
|
||||||
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String deviceModelId(final int deviceIndex) {
|
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());
|
LibVideoCapture.INSTANCE.vcavf_get_device_model_id(deviceIndex, bufferP, buffer.capacity());
|
||||||
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String deviceName(final int deviceIndex) {
|
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());
|
LibVideoCapture.INSTANCE.vcavf_get_device_name(deviceIndex, bufferP, buffer.capacity());
|
||||||
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String deviceFormat(final int deviceIndex, final int formatIndex) {
|
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());
|
LibVideoCapture.INSTANCE.vcavf_get_device_format(deviceIndex, formatIndex, bufferP, buffer.capacity());
|
||||||
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,7 +109,7 @@ public class AVFVideoDevice implements WebcamDeviceExtended {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final var lib = LibVideoCapture.INSTANCE;
|
final LibVideoCapture lib = LibVideoCapture.INSTANCE;
|
||||||
final int authStatus = lib.vcavf_has_videocapture_auth();
|
final int authStatus = lib.vcavf_has_videocapture_auth();
|
||||||
|
|
||||||
if (authStatus != STATUS_AUTHORIZED) {
|
if (authStatus != STATUS_AUTHORIZED) {
|
||||||
|
@ -261,7 +261,7 @@ public class AVFVideoDevice implements WebcamDeviceExtended {
|
||||||
// Init buffer if still not initialized:
|
// Init buffer if still not initialized:
|
||||||
this.bytesPerRow = LibVideoCapture.INSTANCE.vcavf_frame_bytes_per_row(deviceIndex);
|
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.imgBuffer = ByteBuffer.allocateDirect(bufferSizeBytes);
|
||||||
this.arrayByteBuffer = new byte[imgBuffer.capacity()];
|
this.arrayByteBuffer = new byte[imgBuffer.capacity()];
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ public class NokhwaDriver implements WebcamDriver {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized List<WebcamDevice> getDevices() {
|
public synchronized List<WebcamDevice> getDevices() {
|
||||||
final var lib = LibNokhwa.INSTANCE;
|
final LibNokhwa lib = LibNokhwa.INSTANCE;
|
||||||
|
|
||||||
final List<WebcamDevice> list = new ArrayList<>();
|
final List<WebcamDevice> list = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -86,19 +86,19 @@ public class NokhwaDriver implements WebcamDriver {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String deviceUniqueId(final int deviceIndex) {
|
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());
|
LibNokhwa.INSTANCE.cnokhwa_device_unique_id(deviceIndex, bufferP, buffer.capacity());
|
||||||
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String deviceName(final int deviceIndex) {
|
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());
|
LibNokhwa.INSTANCE.cnokhwa_device_name(deviceIndex, bufferP, buffer.capacity());
|
||||||
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String deviceFormatType(final int deviceIndex, final int formatIndex) {
|
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());
|
LibNokhwa.INSTANCE.cnokhwa_device_format_type(deviceIndex, formatIndex, bufferP, buffer.capacity());
|
||||||
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
return bufferP.getString(0, StandardCharsets.UTF_8.name());
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,7 +108,7 @@ public class NokhwaVideoDevice implements WebcamDeviceExtended {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final var lib = LibNokhwa.INSTANCE;
|
final LibNokhwa lib = LibNokhwa.INSTANCE;
|
||||||
final int authStatus = lib.cnokhwa_has_videocapture_auth();
|
final int authStatus = lib.cnokhwa_has_videocapture_auth();
|
||||||
|
|
||||||
if (authStatus != STATUS_AUTHORIZED) {
|
if (authStatus != STATUS_AUTHORIZED) {
|
||||||
|
@ -240,7 +240,7 @@ public class NokhwaVideoDevice implements WebcamDeviceExtended {
|
||||||
|
|
||||||
final PixelWriter pw = writableImage.getPixelWriter();
|
final PixelWriter pw = writableImage.getPixelWriter();
|
||||||
|
|
||||||
final ByteBuffer readBuffer = imgBuffer.asReadOnlyBuffer().position(0);
|
final ByteBuffer readBuffer = (ByteBuffer) imgBuffer.asReadOnlyBuffer().position(0);
|
||||||
pw.setPixels(
|
pw.setPixels(
|
||||||
0, 0, videoWidth, videoHeight,
|
0, 0, videoWidth, videoHeight,
|
||||||
PixelFormat.getByteRgbInstance(), readBuffer, bytesPerRow
|
PixelFormat.getByteRgbInstance(), readBuffer, bytesPerRow
|
||||||
|
@ -256,7 +256,7 @@ public class NokhwaVideoDevice implements WebcamDeviceExtended {
|
||||||
// Init buffer if still not initialized:
|
// Init buffer if still not initialized:
|
||||||
this.bytesPerRow = LibNokhwa.INSTANCE.cnokhwa_frame_bytes_per_row(deviceIndex);
|
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.imgBuffer = ByteBuffer.allocateDirect(bufferSizeBytes);
|
||||||
this.arrayByteBuffer = new byte[imgBuffer.capacity()];
|
this.arrayByteBuffer = new byte[imgBuffer.capacity()];
|
||||||
|
|
||||||
|
@ -297,7 +297,7 @@ public class NokhwaVideoDevice implements WebcamDeviceExtended {
|
||||||
new int[]{0, 1, 2}
|
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());
|
readBuffer.get(arrayByteBuffer, 0, readBuffer.capacity());
|
||||||
|
|
||||||
final DataBuffer dataBuffer = new DataBufferByte(arrayByteBuffer, arrayByteBuffer.length);
|
final DataBuffer dataBuffer = new DataBufferByte(arrayByteBuffer, arrayByteBuffer.length);
|
||||||
|
|
Loading…
Reference in New Issue