JavaFX 2.2 中的 SVG 图像 [英] SVG Image in JavaFX 2.2

查看:51
本文介绍了JavaFX 2.2 中的 SVG 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JavaFX 2.2 的新手,到目前为止我找不到在我的 JavaFX 2.2 应用程序中显示 SVG 图像的方法.我看了一下 Batik,但它对我没有帮助,因为它转换为 BufferedImages 而不是 javafx.ImageView.

有没有办法在 JavaFX 应用程序中显示 SVG 图像?或者您至少可以从 JavaFX 导出 SVG 图像?函数 Node.snapshot() 是否有任何帮助?

解决方案

有没有办法在 JavaFX 应用程序中显示 SVG 图像?

这里有一些选项:

  1. 创建一个 WebView加载svg 图像到 WebView 的 WebEngine.
  2. e(fx)clipse 项目包括一个 svg 到 fxml 转换器(链接现已失效:().
  3. 这个NetBeans 插件还将 svg 转换为 fxml(链接现已失效:().
  4. 您可以使用基于 awt 的库,例如 Batiksvgsalamander 并将结果 BufferedImage 转换为 JavaFX Image.
  5. JavaFX 2.2 本身包括对一些最小的SVGPath 字符串(不是完整的 SVG 规范).
  6. 您可以编写一个使用 JavaFX Canvas 呈现 svg 的转换器 GraphicsContext 命令.
  7. FranzXaver 提供了一个 SVGLoader,其用法在以下回答中演示:在 JavaFX 上的按钮中加载 SVG 文件

<块引用>

或者您至少可以从 javafx 导出 SVG 图像吗?

此功能听起来像是 JavaFX SceneGraph 到 svg 转换器.虽然理论上可行,但我不知道有人已经创建了这样的工具.

<块引用>

函数 Node.snapshot() 对那里有任何帮助吗?

Node.snapshot() 将无助于从 JavaFX 场景图中导出 svg 图像,因为 svg 是基于矢量的格式,而节点快照是位映射格式.

<块引用>

我看了一下 Batik,但它对我没有帮助,因为它转换为 BufferedImages 而不是 javafx.ImageView.

您可以使用 SwingFXUtils.

<块引用>

在性能方面 - 您会推荐哪种方式?

对于复杂的 svg,与从 fxml 渲染相比,您可能会获得更好的渲染到位图图像或画布图形上下文的性能.这是因为您的场景图最终会简单得多,节点也少得多——这意味着 javafx 运行时要做的工作会少得多.对于简单的 svgs(生成 <1000 个节点),它可能不会有太大影响.

<块引用>

是否将 SVG 转换为 FXML 以创建 javafx.scene.shape.SVGPath 的实例?

部分.完整的SVG 规范涵盖了比基本形状更多的内容.此外,SVG 可以执行渐变、效果、动画等.因此,如果源 SVG 图像包含这些附加项,那么它们也需要转换为 FXML(尽其所能,SVG 的某些方面(例如脚本)将难以转换为 FXML).

我的猜测是 SVG 规范的大小和复杂性是当前 JavaFX 核心 API 中没有包含对完整 SVG 图像系统的直接支持的原因之一,相反,只有有限的类似功能,例如 SVGPath提供.与其他理想的项目相比,在 JavaFX 核心 API 中本地实现 SVG 的成本较高,但回报相对较少.

<块引用>

如果是这样,#2(和#3)和#5 会不会是一样的?

没有.#2 和 #3 中引用的 NetBeans 和 Eclipse 工具比 #5 中提到的 SVGPath 工具更实用,因为这些工具将 SVG 规范的其他方面转换为 FXML 等效项(例如渐变、效果和转换).

I am new to JavaFX 2.2 and as of now I couldn't find a way to display SVG Images in my JavaFX 2.2 application. I took a look at Batik, but it didn't do the trick for me as it converts to BufferedImages and not to javafx.ImageView.

Is there any way to display an SVG image in an JavaFX application? Or can you at least export an SVG image from JavaFX? Does the function Node.snapshot() help there in any way?

解决方案

Is there any way to display an SVG image in an JavaFX application?

Here are some options:

  1. Create a WebView and load the svg image into the WebView's WebEngine.
  2. The e(fx)clipse project includes an svg to fxml converter (link now dead :().
  3. This NetBeans plugin also converts svg to fxml (link now dead :().
  4. You can use an awt based library like Batik or svgsalamander and convert the resultant BufferedImage to a JavaFX Image.
  5. JavaFX 2.2 natively includes support for some minimal SVGPath strings (not the full SVG spec).
  6. You could write a convertor which renders the svg using JavaFX Canvas GraphicsContext commands.
  7. FranzXaver provides an SVGLoader, whose usage is demonstrated in the answer to: Load SVG file in a Button on JavaFX

Or can you at least export an SVG image from javafx?

This feature sounds like a JavaFX SceneGraph to svg converter. While it is theoretically possible, I am not aware that anybody has created such a tool yet.

Does the function Node.snapshot() help there in any way?

Node.snapshot() will not help with exporting an svg image from a JavaFX scene graph as svg is a vector based format and a node snapshot is a bit mapped format.

I took a look at Batik, but it didn't do the trick for me as it converts to BufferedImages and not to javafx.ImageView.

You can easily convert between awt BufferedImages and javafx images using SwingFXUtils.

In terms of performance - which way would you recommend?

For complex svg's, rather than rendering from fxml, you will probably get better performance rendering to a bitmapped image or canvas graphics context. This is because your scene graph will end up a lot simpler, with far less nodes - which means that the javafx runtime will have a lot less work to do. For simple svgs (< 1000 nodes generated), it probably won't matter too much.

Is converting an SVG to an FXML about creating instances of javafx.scene.shape.SVGPath?

Partially. The full SVG specification covers much more ground than the basic shapes. In addition gradients, effects, animations, etc can be performed by an SVG. So if the source SVG image includes those additional items, then they also need to be converted to FXML (as best they can be, there will be some aspects of SVG such as scripting which would be hard to translate to FXML).

My guess is that the size and complexity of the SVG specification is one of the reasons why no direct support for the full SVG image system is currently included in the JavaFX core API and, instead, only limited similar functionality such as SVGPath is offered. Implementing SVG natively in the JavaFX core API would have been high cost for relatively little reward when compared to other desirable items.

If that is the case, wouldn't #2 (and #3) and #5 be just the same?

No. The NetBeans and Eclipse tools referenced in #2 and #3 were more functional than just the SVGPath facilities mentioned in #5, as those tools converted additional aspects of the the SVG specification to FXML equivalents (e.g. gradients, effects and transforms).

这篇关于JavaFX 2.2 中的 SVG 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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