gpt4 book ai didi

java - 保存 JavaFX 快照时获取黑色图像

转载 作者:行者123 更新时间:2023-11-30 03:18:35 24 4
gpt4 key购买 nike

我设置了一个后台任务,在给定的面板/图表变得可见后等待几秒钟。这是通过在后台非 GUI 线程上运行 sleep 来执行的,然后在唤醒时运行

Platform.runLater

创建快照和图像。

在保存图像的真正“操作”发生之前,我们可以看到窗口出现:

enter image description here

当该图像正在渲染时,我们的后台代码已被任务置于 sleep 状态。 5000 毫秒后,后台任务将唤醒并启动 Platform.runLater 将场景/ Pane /图表保存到文件中。

这是快照和图像代码:

所有这些都通过提交到ThreadPoolTask在后台线程上发生

    Thread.sleep(5000)   // Wait for images to be rendered -
// they are visually confirmed to be available at about 1000 ms actually
javafx.application.Platform.runLater(new Runnable() {
override def run() = {
// val snapShot = chart.snapshot(null)
// val snapShot = scene.snapshot(null)
val snapShot = pane.snapshot(null,null)
ImageIO.write(SwingFXUtils.fromFXImage(snapShot, null),
"jpg", new File(fileName))

正如您所看到的(从注释掉的行中) - 我对使用哪个对象来创建快照感到困惑:以上三个对象都已尝试过:

  • 图表
  • 场景
  • Pane

结果始终是黑色图像。 OOC 我也尝试通过

更改背景颜色
snapshotParameters.setFill(Color.WHITE)

这没有效果。

enter image description here

正确的程序是什么?

更新我还尝试了回调方法:

        pane.snapshot(  // Also tried scene and chart here ..
new Callback[SnapshotResult, Void]() {
override def call(result: SnapshotResult): Void = {
ImageIO.write(SwingFXUtils.fromFXImage(result.getImage, null),
"jpg", new File(fileName))
latch.countDown
null
}
},p,null)

同样 - 仍然是黑色图像。

最佳答案

使用这种启发法是相当危险的。但是,您没有提供完整的 MCVE。如果您想这样做,您可以使用 PauseTransition,对任何节点进行快照,然后将图像保存到文件中。

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

import javax.imageio.ImageIO;

public class AutoSnapshot extends Application {

private static String fileName = "c:/temp/image.jpg";
private static Color backgroundColor = Color.WHITE;

@Override
public void start(Stage primaryStage) {

AnchorPane root = new AnchorPane();

// dummy chart
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Number of Month");
final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);
lineChart.setTitle("Stock Monitoring, 2010");
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
series.getData().add(new XYChart.Data(1, 23));
series.getData().add(new XYChart.Data(2, 14));
series.getData().add(new XYChart.Data(3, 15));
series.getData().add(new XYChart.Data(4, 24));
lineChart.getData().add(series);

root.getChildren().add( lineChart);

Scene scene = new Scene(root, 800, 600, backgroundColor);
primaryStage.setScene(scene);
primaryStage.show();

Duration delay = Duration.seconds(5);
PauseTransition pt = new PauseTransition(delay);
pt.setOnFinished(e -> {
saveSnapshot(lineChart);
});
pt.play();

}

public static Image createImage(Node node) {

WritableImage wi;

SnapshotParameters parameters = new SnapshotParameters();
parameters.setFill(backgroundColor);

int imageWidth = (int) node.getBoundsInLocal().getWidth();
int imageHeight = (int) node.getBoundsInLocal().getHeight();

wi = new WritableImage(imageWidth, imageHeight);
node.snapshot(parameters, wi);

return wi;

}

private static void saveSnapshot(Node node) {

Image image = createImage(node);

// save image !!! has bug because of transparency (use approach below) !!!
// ImageIO.write(SwingFXUtils.fromFXImage( selectedImage.getImage(), null), "jpg", file);

// save image (without alpha)
BufferedImage bufImageARGB = SwingFXUtils.fromFXImage(image, null);
BufferedImage bufImageRGB = new BufferedImage(bufImageARGB.getWidth(), bufImageARGB.getHeight(), BufferedImage.OPAQUE);

Graphics2D graphics = bufImageRGB.createGraphics();
graphics.drawImage(bufImageARGB, 0, 0, null);

try {
ImageIO.write(bufImageRGB, "jpg", new File(fileName));
} catch (IOException e) {
e.printStackTrace();
}

graphics.dispose();

System.out.println( "Image saved: " + fileName);

}

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

关于java - 保存 JavaFX 快照时获取黑色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31909297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com