SwingFXUtils 替代图像序列化(Javafx、Swing、Raspberry Pi)

2022-01-17 00:00:00 raspberry-pi arm java swing javafx

我的 JavaFX 应用程序的一个用例是在一侧加载图像,通过 TCP 套接字对其进行序列化,以在另一侧将其显示为 JavaFX 图像.

One of the use cases of my JavaFX application is to load an image on one side, serialize it over a TCP socket to show it as JavaFX image on the other side.

为了实现这一点,我使用 SwingFXUtils.fromFXImage()SwingFXUtils.toFXImage() 来创建和读取一个 BufferedImage 可以是序列化.

To implement this I am using SwingFXUtils.fromFXImage() and SwingFXUtils.toFXImage() to create and read a BufferedImage which can be serialized.

一切正常.但我想在树莓派上运行显示面.我发现,在 ARM 上的 JavaFX 中没有集成 Swing 组件,因此在 Raspi 上使用 SwingFXUtils 时会得到 NoClassDefFoundError.

Everything is working. But I would like to run the displaying side on a raspberry pi. As I found out, there is no integration of Swing components in JavaFX on ARM, so I get a NoClassDefFoundError when using SwingFXUtils on a Raspi.

请建议我如何在不使用 SwingFXUtils 的情况下创建和读取可序列化的图像对象?

Please suggest how I can create and read a serializable image object without the use of SwingFXUtils?

推荐答案

感谢您的回答.

@haraldK 当然,我在序列化之前已经将我的 BufferedImage 写入了 ByteArray.我昨晚的解释不够准确.过失.

@haraldK of course i have written my BufferedImage to a ByteArray before serialization. My explanation last night wasn't exact enough. Mea culpa.

我找到了解决问题的方法:为了让它在 Raspberry Pi 上工作,我只是用 InputStream 替换了 BufferedImage 来读取我的 ByteArray 包含图像数据.这允许我使用 InputStream 而不是使用 SwingFXUtils 创建 JavaFX Image 对象.

I found a solution for my problem: To make it work on a Raspberry Pi I just substituted BufferedImage with an InputStream to read my ByteArray containing the image data. This allowed me to create the JavaFX Image object with an InputStream instead of using SwingFXUtils.

之前:

BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imageUpdate.getImageByteArray()));

imageView.setImage(SwingFXUtils.toFXImage(bufferedImage, null));

,其中 imageUpdate 是一个 ImageUpdate 对象,用于通过 TCP 套接字传输数据(包含图像和附加数据).

, where imageUpdate is an ImageUpdate Object used for data transport over TCP sockets (containing image and additional data).

如前所述,这会产生

Exception in thread "..." java.lang.NoClassDefFoundError: javafx/embed/swing/JFXPanel

在 Raspberry Pi 上,因为 ARM 架构的 JavaFX 端口中缺少 Swing 类.

on the Raspberry Pi because of missing Swing Classes in JavaFX Port for ARM architecture.

之后:

InputStream inputStream = new ByteArrayInputStream(imageUpdate.getImageByteArray());

imageView.setImage(new Image(inputStream));

这个解决方案现在就像我的 Raspberry Pi 上的魅力一样.

This solution works now like a charm on my Raspberry Pi.

相关文章