使用Java FX在服务器端生成映像 [英] Generating image at server side using Java FX

查看:157
本文介绍了使用Java FX在服务器端生成映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在研究Jax Rs应用程序,我想将Base64编码的图像输出到客户端。客户端是移动设备。

Currently I'm working on Jax Rs application and I want to output Base64 encoded image to the client. The client is a mobile device.

移动设备将使用一些参数调用此服务,服务器必须绘制条形图并将其作为base64编码的图像字符串。

The mobile device will call this service with some parameters and the server has to draw a bar chart and send it back to the device as a base64 encoded image string.

由于java Fx具有所需的图表库,我使用以下教程做了一个示例。 快照功能也按预期正常工作(创建屏幕图像)。

Since java Fx having the required chart libraries, I did a sample using following tutorial. "snapshot" function also worked correctly as expected (to create a image of the screen).

http://docs.oracle.com/javafx/2/charts/bar-chart.htm#CIHJFHDE

现在我想在不扩展Application类的情况下这样做,因为我需要在Jax Rs应用程序中使用它。这样我就可以使用api创建一个BuffredImage然后用它来创建Base64字符串。

Now I want to do this without extending Application class because I need to this within the Jax Rs application. So that I can just use api to create a BuffredImage and then use that for create Base64 string.

我找到了一种使用JFreeChart做到这一点的方法。但我更喜欢我能用Java FX做到这一点。我以前没有任何Java Fx经验

I found a way to do this using JFreeChart. But I prefer if I can do this using Java FX. I do not having any previous experience with Java Fx

请建议

推荐答案

基于服务器的JavaFX运行时初始化

要在服务器上运行JavaFX,您需要:

To run JavaFX on a server you need to either:


  1. 启动JavaFX 申请

  2. 使用 JFXPanel

  1. Launch a JavaFX Application OR
  2. Use a JFXPanel.

这些是在JavaFX 2中初始化JavaFX运行时系统的唯一方法,以便您可以使用它。

Those are the only ways to get the JavaFX runtime system initialized in JavaFX 2 so that you can use it.

与使用JavaFX应用程序相比,使用JFXPanel的处理效率可能略低。

Using a JFXPanel is probably slightly less efficient processing wise than using a JavaFX Application.

有关StackOverflow问题中JavaFX系统初始化的进一步讨论: JavaFX 2.1:Toolkit未初始化

There is further discussion on initialization of the JavaFX system in the StackOverflow question: JavaFX 2.1: Toolkit not initialized.

JavaFX是一个单线程系统

您可以在任何线程中创建大多数JavaFX组件。但是,要在场景中呈现组件,必须在JavaFX Application线程上执行工作。这意味着如果您有一个多线程服务器进程(大多数服务器都是这样,并且您想要生成多个图表),则需要使用并发约束单独绘制图表呈现请求。

You can create most JavaFX components in any thread. However to render components in a scene, you must perform the work on the JavaFX Application thread. This means that if you have a multi-threaded server process, which most servers are, and you want to generate multiple charts, you will need to single thread the chart rendering requests using concurrency constraints.


  1. 当您收到图表的传入请求时,请发出 Platform.runLater 命令。 runLater块中的所有代码都将放在最终将在JavaFX应用程序线程上运行的队列中。

  2. 在runLater块中,为图表创建一个场景并将其快照到图像。 快照的回调版本可能是最适合在这里使用的版本,因为它可能不会将JavaFX应用程序线程尽可能多地捆绑在一起,尽管可能会使用它。

  3. 使用 SwingFXUtils.fromFXImage

  4. 获取将您的图像结果返回到服务器处理程序线程中,使用 FutureTask 技术:从javafx平台runlater返回结果

  1. When you get an incoming request for a chart, issue a Platform.runLater command. All code in the runLater block will be placed in a queue that will eventually run on the JavaFX application thread.
  2. In the runLater block create a scene for your chart and snapshot it to an image. The callback version of snapshot might be the most appropriate one to use here as it likely doesn't tie up the JavaFX Application Thread as much, though it is likely tricker to use.
  3. Convert the JavaFX image to a AWT image using SwingFXUtils.fromFXImage.
  4. To get your image result back in your server handler thread, use the FutureTask technique outlined by sarcan in: Return result from javafx platform runlater.

您的服务器处理程序线程可以使用 ImageIO 进行转换AWT图像以png等格式输出到输出流。您可以获取结果流和 Base64对其进行编码并让服务器返回基本64位编码的流以响应原始图像请求调用。

Your server handler thread can then use ImageIO to convert the AWT image to an output stream in a format like png. You can take the result stream and Base64 encode it and have the server return the base 64 encoded stream in response to the original image request call.

确保正常关闭

您需要调用 Platform.setImplicitExit(false) /javaee/6/api/javax/servlet/ServletContextListener.htmlrel =nofollow noreferrer> ServletContextListener 监视servlet何时被销毁,以便您也调用 Platform.exit()关闭在JavaFX系统中。如果你不这样做,可能你的服务器将无法干净地关闭,因为JavaFX应用程序线程将继续运行等待工作。

You will want to invoke Platform.setImplicitExit(false) when your server starts up and register a shutdown hook or a ServletContextListener that monitors when the servlet is being destroyed, so that you also invoke Platform.exit() to shutdown the JavaFX system. If you don't do this, likely your server won't be able to shut down cleanly because the JavaFX application thread will continue running awaiting work to do.

JavaFX 2.2并未真正认证可以在无头服务器上运行

Swing应用程序可以使用系统属性在无头模式下运行 java.awt.headless 。我不知道JavaFX的类似属性,虽然可能有一个,如果有的话,你可以通过询问 openjfx-dev邮件列表

Swing applications can run in headless mode using a system property java.awt.headless. I am not aware of a similar property for JavaFX, though there may one and, if there were, you might find out what it was by asking the openjfx-dev mailing list.

JavaFX主要设计为客户端图形工具包。虽然您可以在服务器上运行并运行令人满意的应用程序,但为此,您可能需要确保服务器未设置为无头服务器,它有一个合适的图形加速卡,可以在负载下提供合理的性能。

JavaFX is primarily designed as a client graphics toolkit. While you might get it to work and run satisfactorily for your application on a server, to do so, you may need to ensure that the server is not setup as a headless server and that it has an appropriate graphic accelerator card to provide reasonable performance under load.

你可以提交要求在 JavaFX问题跟踪器中正式支持无头模式。

You can file a request for official support of a headless mode in the JavaFX issue tracker.

这篇关于使用Java FX在服务器端生成映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆