[英]Generating image at server side using Java FX
Currently I'm working on Jax Rs application and I want to output Base64 encoded image to the client. 目前我正在研究Jax Rs应用程序,我想将Base64编码的图像输出到客户端。 The client is a mobile device.
客户端是移动设备。
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. 移动设备将使用一些参数调用此服务,服务器必须绘制条形图并将其作为base64编码的图像字符串发送回设备。
Since java Fx having the required chart libraries, I did a sample using following tutorial. 由于java Fx具有所需的图表库,我使用以下教程做了一个示例。 "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 http://docs.oracle.com/javafx/2/charts/bar-chart.htm#CIHJFHDE
Now I want to do this without extending Application class because I need to this within the Jax Rs application. 现在我想在不扩展Application类的情况下这样做,因为我需要在Jax Rs应用程序中使用它。 So that I can just use api to create a BuffredImage and then use that for create Base64 string.
这样我就可以使用api创建一个BuffredImage然后用它来创建Base64字符串。
I found a way to do this using JFreeChart. 我找到了一种使用JFreeChart做到这一点的方法。 But I prefer if I can do this using Java FX.
但我更喜欢我能用Java FX做到这一点。 I do not having any previous experience with Java Fx
我没有任何Java Fx的经验
Please advice 请指教
Server based JavaFX runtime Initialization 基于服务器的JavaFX运行时初始化
To run JavaFX on a server you need to either: 要在服务器上运行JavaFX,您需要:
Those are the only ways to get the JavaFX runtime system initialized in JavaFX 2 so that you can use it. 这些是在JavaFX 2中初始化JavaFX运行时系统的唯一方法,以便您可以使用它。
Using a JFXPanel is probably slightly less efficient processing wise than using a JavaFX Application. 与使用JavaFX应用程序相比,使用JFXPanel处理效率可能略低。
There is further discussion on initialization of the JavaFX system in the StackOverflow question: JavaFX 2.1: Toolkit not initialized . 在StackOverflow问题中进一步讨论了JavaFX系统的初始化: JavaFX 2.1:Toolkit未初始化 。
JavaFX is a single threaded system JavaFX是一个单线程系统
You can create most JavaFX components in any thread. 您可以在任何线程中创建大多数JavaFX组件。 However to render components in a scene, you must perform the work on the JavaFX Application thread.
但是,要在场景中呈现组件,必须在JavaFX Application线程上执行工作。 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.
这意味着如果您有一个多线程服务器进程(大多数服务器都是这样,并且您想要生成多个图表),则需要使用并发约束单独绘制图表呈现请求。
Your server handler thread can then use ImageIO to convert the AWT image to an output stream in a format like png. 然后,您的服务器处理程序线程可以使用ImageIO将AWT图像转换为输出流,格式为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.
您可以获取结果流并对其进行Base64编码,并让服务器返回基本64位编码的流以响应原始图像请求调用。
Ensure graceful shutdown 确保正常关机
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. 您需要在服务器启动时调用Platform.setImplicitExit(false)并注册一个关闭钩子或ServletContextListener来监视servlet何时被销毁,以便您还调用Platform.exit()来关闭JavaFX系统。 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应用程序线程将继续运行等待工作。
JavaFX 2.2 is not really certified to run on a headless server JavaFX 2.2并未真正经过认证,无法在无头服务器上运行
Swing applications can run in headless mode using a system property java.awt.headless . Swing应用程序可以使用系统属性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的类似属性,虽然可能有一个,如果有的话,你可以通过询问openjfx-dev邮件列表找出它是什么。
JavaFX is primarily designed as a client graphics toolkit. JavaFX主要设计为客户端图形工具包。 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.
虽然您可以在服务器上运行并运行满意的应用程序,但是您可能需要确保服务器不是设置为无头服务器 ,并且它具有适当的图形加速卡以提供合理的性能。加载。
You can file a request for official support of a headless mode in the JavaFX issue tracker . 您可以在JavaFX问题跟踪器中提交正式支持无头模式的请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.