improve capture display efficiency, fix resizing bug and refactor

This commit is contained in:
Craig Raw 2025-03-13 13:52:01 +02:00
parent 3e197eb310
commit 2c4de99fad
2 changed files with 28 additions and 21 deletions

View file

@ -98,21 +98,26 @@ public class QRScanDialog extends Dialog<QRScanDialog.Result> {
this.webcamService = new WebcamService(webcamResolutionProperty.get(), null); this.webcamService = new WebcamService(webcamResolutionProperty.get(), null);
webcamService.setPeriod(Duration.millis(SCAN_PERIOD_MILLIS)); webcamService.setPeriod(Duration.millis(SCAN_PERIOD_MILLIS));
webcamService.setRestartOnFailure(false); webcamService.setRestartOnFailure(false);
WebcamView webcamView = new WebcamView(webcamService, Config.get().isMirrorCapture());
final DialogPane dialogPane = new QRScanDialogPane(); final DialogPane dialogPane = new QRScanDialogPane();
setDialogPane(dialogPane); setDialogPane(dialogPane);
AppServices.setStageIcon(dialogPane.getScene().getWindow()); AppServices.setStageIcon(dialogPane.getScene().getWindow());
StackPane stackPane = new StackPane(); WebcamView webcamView = new WebcamView(webcamService, Config.get().isMirrorCapture());
stackPane.getChildren().add(webcamView.getView());
Node wrappedView = Borders.wrap(stackPane).lineBorder().buildAll();
ProgressBar progressBar = new ProgressBar(); ProgressBar progressBar = new ProgressBar();
progressBar.setMinHeight(20); progressBar.setMinHeight(20);
progressBar.setPadding(new Insets(0, 10, 0, 10)); progressBar.setPadding(new Insets(0, 10, 0, 10));
progressBar.setPrefWidth(Integer.MAX_VALUE); progressBar.setPrefWidth(Integer.MAX_VALUE);
progressBar.progressProperty().bind(percentComplete); progressBar.progressProperty().bind(percentComplete);
VBox vBox = new VBox(20);
StackPane stackPane = new StackPane();
stackPane.getChildren().add(webcamView.getView());
Node wrappedView = Borders.wrap(stackPane).lineBorder().buildAll();
vBox.getChildren().addAll(wrappedView, progressBar);
dialogPane.setContent(vBox);
webcamService.openingProperty().addListener((_, _, opening) -> { webcamService.openingProperty().addListener((_, _, opening) -> {
if(percentComplete.get() <= 0.0) { if(percentComplete.get() <= 0.0) {
Platform.runLater(() -> percentComplete.set(opening ? 0.0 : -1.0)); Platform.runLater(() -> percentComplete.set(opening ? 0.0 : -1.0));
@ -155,24 +160,25 @@ public class QRScanDialog extends Dialog<QRScanDialog.Result> {
}); });
} }
}); });
VBox vBox = new VBox(20);
vBox.getChildren().addAll(wrappedView, progressBar);
dialogPane.setContent(vBox);
webcamService.resultProperty().addListener(new QRResultListener()); webcamService.resultProperty().addListener(new QRResultListener());
webcamService.setOnFailed(failedEvent -> { webcamService.setOnFailed(failedEvent -> {
Throwable exception = Throwables.getRootCause(failedEvent.getSource().getException()); Throwable exception = Throwables.getRootCause(failedEvent.getSource().getException());
Platform.runLater(() -> setResult(new Result(exception))); Platform.runLater(() -> setResult(new Result(exception)));
}); });
webcamService.start(); webcamService.start();
webcamResolutionProperty.addListener((_, oldResolution, newResolution) -> { webcamResolutionProperty.addListener((_, oldResolution, newResolution) -> {
if(newResolution != null) { if(newResolution != null) {
if(newResolution.isStandardAspect() && oldResolution.isWidescreenAspect()) { if(newResolution.isStandardAspect() && oldResolution.isWidescreenAspect()) {
setHeight(getHeight() + 100); setHeight(getHeight() + 100);
dialogPane.setMaxHeight(dialogPane.getPrefHeight() + 100);
dialogPane.setPrefHeight(dialogPane.getMaxHeight());
dialogPane.setMinHeight(dialogPane.getMaxHeight());
} else if(newResolution.isWidescreenAspect() && oldResolution.isStandardAspect()) { } else if(newResolution.isWidescreenAspect() && oldResolution.isStandardAspect()) {
setHeight(getHeight() - 100); setHeight(getHeight() - 100);
dialogPane.setMaxHeight(dialogPane.getPrefHeight() - 100);
dialogPane.setPrefHeight(dialogPane.getMaxHeight());
dialogPane.setMinHeight(dialogPane.getMaxHeight());
} }
EventManager.get().post(new WebcamResolutionChangedEvent(newResolution)); EventManager.get().post(new WebcamResolutionChangedEvent(newResolution));
} }

View file

@ -57,27 +57,29 @@ public class WebcamView {
this.view = new Region() { this.view = new Region() {
{ {
service.stateProperty().addListener((obs, oldState, newState) -> { service.stateProperty().addListener((obs, oldState, newState) -> {
switch (newState) { switch(newState) {
case READY: case READY:
if(imageProperty.get() == null) { if(imageProperty.get() == null) {
statusPlaceholder.setText("Initializing"); statusPlaceholder.setText("Initializing");
getChildren().setAll(statusPlaceholder); getChildren().setAll(statusPlaceholder);
} }
break ; break;
case SCHEDULED: case SCHEDULED:
if(imageProperty.get() == null) { if(imageProperty.get() == null) {
statusPlaceholder.setText("Waiting"); statusPlaceholder.setText("Waiting");
getChildren().setAll(statusPlaceholder); getChildren().setAll(statusPlaceholder);
} }
break ; break;
case RUNNING: case RUNNING:
if(imageProperty.get() == null) {
imageView.imageProperty().unbind(); imageView.imageProperty().unbind();
imageView.imageProperty().bind(imageProperty); imageView.imageProperty().bind(imageProperty);
getChildren().setAll(imageView); getChildren().setAll(imageView);
break ; }
break;
case CANCELLED: case CANCELLED:
imageProperty.set(null);
imageView.imageProperty().unbind(); imageView.imageProperty().unbind();
imageView.setImage(null);
statusPlaceholder.setText("Stopped"); statusPlaceholder.setText("Stopped");
getChildren().setAll(statusPlaceholder); getChildren().setAll(statusPlaceholder);
break; break;
@ -93,7 +95,6 @@ public class WebcamView {
statusPlaceholder.setText(""); statusPlaceholder.setText("");
getChildren().clear(); getChildren().clear();
} }
requestLayout();
}); });
} }
@ -102,14 +103,14 @@ public class WebcamView {
super.layoutChildren(); super.layoutChildren();
double w = getWidth(); double w = getWidth();
double h = getHeight(); double h = getHeight();
if (service.isRunning()) { if(service.isRunning()) {
imageView.setFitWidth(w); imageView.setFitWidth(w);
imageView.setFitHeight(h); imageView.setFitHeight(h);
imageView.resizeRelocate(0, 0, w, h); imageView.resizeRelocate(0, 0, w, h);
} else { } else {
double labelHeight = statusPlaceholder.prefHeight(w); double labelHeight = statusPlaceholder.prefHeight(w);
double labelWidth = statusPlaceholder.prefWidth(labelHeight); double labelWidth = statusPlaceholder.prefWidth(labelHeight);
statusPlaceholder.resizeRelocate((w - labelWidth)/2, (h-labelHeight)/2, labelWidth, labelHeight); statusPlaceholder.resizeRelocate((w - labelWidth) / 2, (h - labelHeight) / 2, labelWidth, labelHeight);
} }
} }