尝试将javafx WebView渲染到屏幕外缓冲区或FBO

问题描述 投票:2回答:1

最终的目标是能够以30fps或更高的速度记录WebView的输出,也许是通过为javafx设置FBO?然后我可以以我想要的任何帧率拉出帧。

我捅了一些,我在ViewScene中遇到了UploadingPainter,这让我觉得这是可能的。斗争是,这似乎是在幕后,对我来说有点新鲜。

有人知道如何制作这样的作品?

这是我在调试过程中遇到的代码:

@Override
public void setStage(GlassStage stage) {
    super.setStage(stage);
    if (stage != null) {
        WindowStage wstage  = (WindowStage)stage;
        if (wstage.needsUpdateWindow() || GraphicsPipeline.getPipeline().isUploading()) {
            if (Pixels.getNativeFormat() != Pixels.Format.BYTE_BGRA_PRE ||
                ByteOrder.nativeOrder() != ByteOrder.LITTLE_ENDIAN) {
                throw new UnsupportedOperationException(UNSUPPORTED_FORMAT);
            }
            painter = new UploadingPainter(this);
        } else {
            painter = new PresentingPainter(this);
        }
        painter.setRoot(getRoot());
        paintRenderJob = new PaintRenderJob(this, PaintCollector.getInstance().getRendered(), painter);
    }
}
javafx jogl
1个回答
3
投票

以下是在WebView中捕获动画的示例。

从Web视图捕获的图像被放置在Paginator中以供查看,以便于查看它们。如果您愿意,可以使用SwingFXUtilsImageIO将它们写入文件。如果您想将结果图像放入缓冲区,可以使用它们的PixelReader

first second

它不像我想要的那样工作。我想快照WebView而不将其置于可见的阶段。拍摄不在舞台上的节点的快照适用于JavaFX中的每个其他节点类型(据我所知),但是,出于某种奇怪的原因,它不适用于WebView。因此,示例实际上在显示窗口后面创建了一个新阶段,显示动画捕获结果的图像序列。我知道不完全是你想要的,但它就是它......

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.collections.*;
import javafx.concurrent.Worker;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewAnimationCaptor extends Application {

    private static final String CAPTURE_URL =
            "https://upload.wikimedia.org/wikipedia/commons/d/dd/Muybridge_race_horse_animated.gif";

    private static final int N_CAPS_PER_SECOND = 10;
    private static final int MAX_CAPTURES = N_CAPS_PER_SECOND * 5;
    private static final int W = 186, H = 124;

    class CaptureResult {
        ObservableList<Image> images = FXCollections.observableArrayList();
        DoubleProperty progress = new SimpleDoubleProperty();
    }

    @Override public void start(Stage stage) {
        CaptureResult captures = captureAnimation(CAPTURE_URL);
        Pane captureViewer = createCaptureViewer(captures);

        stage.setScene(new Scene(captureViewer, W + 40, H + 80));
        stage.show();
    }

    private StackPane createCaptureViewer(CaptureResult captures) {
        ProgressIndicator progressIndicator = new ProgressIndicator();
        progressIndicator.progressProperty().bind(captures.progress);
        progressIndicator.setPrefSize(W, H);

        StackPane stackPane = new StackPane(progressIndicator);
        stackPane.setPadding(new Insets(10));
        if (captures.progress.get() >= 1.0) {
            stackPane.getChildren().setAll(
                createImagePages(captures.images)
            );
        } else {
            captures.progress.addListener((observable, oldValue, newValue) -> {
                if (newValue.doubleValue() >= 1.0) {
                    stackPane.getChildren().setAll(
                            createImagePages(captures.images)
                    );
                }
            });
        }

        return stackPane;
    }

    private Pagination createImagePages(ObservableList<Image> captures) {
        Pagination pagination = new Pagination();
        pagination.setPageFactory(param -> {
            ImageView currentImage = new ImageView();
            currentImage.setImage(
                    param < captures.size()
                            ? captures.get(param)
                            : null
            );

            StackPane pageContent = new StackPane(currentImage);
            pageContent.setPrefSize(W, H);

            return pageContent;
        });

        pagination.setCurrentPageIndex(0);
        pagination.setPageCount(captures.size());
        pagination.setMaxPageIndicatorCount(captures.size());

        return pagination;
    }

    private CaptureResult captureAnimation(final String url) {
        CaptureResult captureResult = new CaptureResult();

        WebView webView = new WebView();
        webView.getEngine().load(url);
        webView.setPrefSize(W, H);

        Stage captureStage = new Stage();
        captureStage.setScene(new Scene(webView, W, H));
        captureStage.show();

        SnapshotParameters snapshotParameters = new SnapshotParameters();
        captureResult.progress.set(0);

        AnimationTimer timer = new AnimationTimer() {
            long last = 0;

            @Override
            public void handle(long now) {
                if (now > last + 1_000_000_000.0 / N_CAPS_PER_SECOND) {
                    last = now;
                    captureResult.images.add(webView.snapshot(snapshotParameters, null));
                    captureResult.progress.setValue(
                            captureResult.images.size() * 1.0 / MAX_CAPTURES
                    );
                }

                if (captureResult.images.size() > MAX_CAPTURES) {
                    captureStage.hide();
                    this.stop();
                }
            }
        };

        webView.getEngine().getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
            if (Worker.State.SUCCEEDED.equals(newValue)) {
                timer.start();
            }
        });

        return captureResult;
    }

    public static void main(String[] args) { launch(args); }
}

要微调动画序列捕获,您可以查看此info on AnimationTimers in JavaFX

如果你需要使这个东西“无头”,那么不需要一个可见的阶段,你可以尝试这个gist by danialfarid which performs "Java Image Capture, HTML Snapshot, HTML to image"(虽然我没有写链接的要点,并没有尝试过)。


在我的情况下,无头是关键。有问题的(linux)机器在服务器场中运行完全无头。至于要点,我在那里看到一个节目(),但我会仔细看看,以确保我没有忽略某些东西。

要点是基于Monocle glass rendering toolkit for JavaFX systems。该工具包支持在任何系统上基于软件的无头渲染。

来自Monocle Documentation

无头端口什么也没做。它适用于您希望运行没有图形,输入或平台依赖性的JavaFX。渲染仍然发生,它只是没有出现在屏幕上。

headless operation

无头端口使用InputDeviceRegistry的LinuxInputDeviceRegistry实现。但是,无头端口根本不访问任何实际的Linux设备或任何本机API;它在设备模拟模式下使用Linux输入注册表。这样即使在非Linux平台上也可以模拟Linux设备输入。 tests / system / src / test / java / com / sun / glass / ui / monocle / input中的测试广泛使用此功能。

如果基于JavaFX Monocle的方法最终没有为您服务,您可以考虑另一个(不是JavaFX相关的)无头HTML呈现工具包,例如PhantomJS

© www.soinside.com 2019 - 2024. All rights reserved.