gpt4 book ai didi

java - 将 GUI 渲染为内存中的图像

转载 作者:行者123 更新时间:2023-12-01 18:23:16 25 4
gpt4 key购买 nike

是否可以将 GUI 渲染到 BufferedImage 或其他类型的内存图像而不将其显示在屏幕上?

我知道这会失去所有类型的硬件加速,但对于每秒仅刷新一次或两次的简单 GUI,这应该不是问题。

已经尝试让 JavaFX 输出图像,但我找不到首先在屏幕上省略渲染的方法。有谁知道如何使用 JavaFX 或 Swing 做到这一点?

使用简单的图像操作自己绘制一个简单的 GUI 是没有问题的,但随后我就必须手动完成这一切,而使用 Swing 或 FX 会让这一切变得更容易。

编辑:为了更清楚一点,我没有 Activity 显示器,但我可以保存图像,然后通过其他方式显示。确切地说,它是一个树莓派,但没有使用 GPIO 端口连接 tft 显示器的主显示设备。因此,我无法将 UI 直接渲染到显示设备,但需要创建一个可以保存在特定位置的图像。到目前为止我尝试过的所有方法都需要主显示设备。

最佳答案

是的,可以将 GUI 渲染到屏幕外的图像。

这是一个使用 JavaFX 的示例,示例图像输出如下:

image

该示例的工作原理是将图表渲染到一个场景,该场景未添加到任何窗口,并且从未显示任何窗口(JavaFX 术语中的“阶段”)。 snapshot方法用于拍摄节点快照,然后 ImageIO实用程序用于将快照保存到磁盘。

如果底层硬件/软件平台支持,屏幕外场景的渲染将进行硬件加速。

import javafx.application.*;
import javafx.collections.*;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.*;
import javafx.scene.chart.PieChart;
import javafx.scene.image.Image;
import javafx.scene.layout.Region;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.*;

public class OffscreenImageRecorder extends Application {
private static final Logger logger = Logger.getLogger(OffscreenImageRecorder.class.getName());

private static final String IMAGE_TYPE = "png";
private static final String IMAGE_FILENAME = "image." + IMAGE_TYPE;
private static final String WORKING_DIR = System.getProperty("user.dir");
private static final String IMAGE_PATH = new File(WORKING_DIR, IMAGE_FILENAME).getPath();

private final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
private final Random random = new Random();

private final int CHART_SIZE = 400;

@Override
public void start(Stage stage) throws IOException {
Parent chart = createChart();
Image image = snapshot(chart);
exportPng(SwingFXUtils.fromFXImage(image, null), IMAGE_PATH);

Platform.exit();
}

private Parent createChart() {
// create a chart.
final PieChart chart = new PieChart();
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Grapefruit", random.nextInt(30)),
new PieChart.Data("Oranges", random.nextInt(30)),
new PieChart.Data("Plums", random.nextInt(30)),
new PieChart.Data("Pears", random.nextInt(30)),
new PieChart.Data("Apples", random.nextInt(30))
);
chart.setData(pieChartData);
chart.setTitle("Imported Fruits - " + dateFormat.format(new Date()));

// It is important for snapshots that the chart is not animated
// otherwise we could get a snapshot of the chart before the
// data display has been animated in.
chart.setAnimated(false);

chart.setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
chart.setPrefSize(CHART_SIZE, CHART_SIZE);
chart.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);

return chart;
}

private Image snapshot(final Parent sourceNode) {
// Note: if the source node is not in a scene, css styles will not
// be applied during a snapshot which may result in incorrect rendering.
final Scene snapshotScene = new Scene(sourceNode);

return sourceNode.snapshot(
new SnapshotParameters(),
null
);
}

private void exportPng(BufferedImage image, String filename) {
try {
ImageIO.write(image, IMAGE_TYPE, new File(filename));

logger.log(Level.INFO, "Wrote image to: " + filename);
} catch (IOException ex) {
logger.log(Level.SEVERE, null, ex);
}
}

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

关于java - 将 GUI 渲染为内存中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27047651/

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