java 在 javafx 2.0 中加载 SVG 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6854601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load SVG file in javafx 2.0
提问by Mr_Qqn
I'd like to display a svg image in javafx 2.0, but I don't find such a thing in the API. I guess it's because it's still in beta.
我想在 javafx 2.0 中显示 svg 图像,但我在 API 中没有找到这样的东西。我猜是因为它仍处于测试阶段。
Until the final release, how can I load a svg ? Is there already a library which can handle that, or do I need to parse myself the file and then create the corresponding shapes ?
在最终版本之前,我如何加载 svg ?是否已经有一个库可以处理这个问题,还是我需要自己解析文件然后创建相应的形状?
Thanks
谢谢
回答by pmoule
Based on the answer of thisquestion I found a working solution.
基于这个问题的答案,我找到了一个可行的解决方案。
1. Include references to the Batik SVG Toolkitjars
1. 包括对Batik SVG Toolkitjars 的引用
2. Implement your own Transcoder
(based on thisanswer by Devon_C_Miller)
2. 实现你自己的转码器
(基于Devon_C_Miller 的这个答案)
class MyTranscoder extends ImageTranscoder {
private BufferedImage image = null;
@Override
public BufferedImage createImage(int w, int h) {
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
return image;
}
@Override
public void writeImage(BufferedImage img, TranscoderOutput out) {
}
public BufferedImage getImage() {
return image;
}
}
}
3. Get a BufferedImage from your svg
(based on hint in thisanswer by John Doppelmann)
3. 从您的 svg 中获取 BufferedImage
(基于John Doppelmann在这个答案中的提示)
String uri = "path_to_svg/some.svg";
MyTranscoder transcoder = new MyTranscoder();
TranscodingHints hints = new TranscodingHints();
hints.put(ImageTranscoder.KEY_WIDTH, 20f); //your image width
hints.put(ImageTranscoder.KEY_HEIGHT, 20f); //your image height
hints.put(ImageTranscoder.KEY_DOM_IMPLEMENTATION, SVGDOMImplementation.getDOMImplementation());
hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, SVGConstants.SVG_NAMESPACE_URI);
hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT, SVGConstants.SVG_SVG_TAG);
hints.put(ImageTranscoder.KEY_XML_PARSER_VALIDATING, false);
transcoder.setTranscodingHints(hints);
TranscoderInput input = new TranscoderInput(url.toExternalForm());
transcoder.transcode(input, null);
BufferedImage bufferedImage = transcoder.getImage();
4. Create an InputStream from BufferedImage
4. 从 BufferedImage 创建一个 InputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JPEGImageEncoder imageEncoder = JPEGCodec.createJPEGEncoder(outputStream);
imageEncoder.encode(bufferedImage);
byte[] bytes = outputStream.toByteArray();
InputStream inputStream = new ByteArrayInputStream(bytes);
5. Add the image to your ImageView
5. 将图像添加到您的 ImageView
//javafx.scene.image.Image
Image image = new Image(inputStream);
//javafx.scene.image.ImageView
ImageView imageView = new ImageView();
imageView.setImage(image);
this.getChildren().add(imageView);
Hope this will help!
希望这会有所帮助!
回答by 8forty
I used @pmoule's answer above, but had better luck replacing his steps 3-5 with this:
我在上面使用了@pmoule 的答案,但最好用这个替换他的步骤 3-5:
MyTranscoder imageTranscoder = new MyTranscoder();
imageTranscoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, (float) width);
imageTranscoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, (float) height);
TranscoderInput input = new TranscoderInput(new FileReader(file));
imageTranscoder.transcode(input, null);
BufferedImage bimage = imageTranscoder.getImage();
WritableImage wimage = SwingFXUtils.toFXImage(bimage, null);
ImageView imageView = new ImageView(wimage);
<panel-vbox-whatever>.getChildren().clear();
<panel-vbox-whatever>.getChildren().add(imageView);
Simpler, and it just worked better for me.
更简单,它对我来说效果更好。
回答by Niklas
回答by Chengwen Bo
you do not need batik, you can try WebView like this:
你不需要蜡染,你可以像这样尝试 WebView:
WebView view = new WebView();
view.setMinSize(500, 400);
view.setPrefSize(500, 400);
final WebEngine eng = view.getEngine();
eng.load("http://127.0.0.1/demo1.svg");
回答by swpalmer
Convert the SVG to FXML and then it becomes trivial. The conversion can be done with XSLT. You can get the stylesheet from here:
将 SVG 转换为 FXML,然后它就变得微不足道了。可以使用 XSLT 完成转换。您可以从这里获取样式表:
http://jayskills.com/blog/2012/06/03/svg-to-fxml-using-netbeans/
http://jayskills.com/blog/2012/06/03/svg-to-fxml-using-netbeans/
Then after conversion to FXML you can load it as a Node with the built-in FXMLLoader:
然后在转换为 FXML 后,您可以使用内置的 FXMLLoader 将其作为节点加载:
FXMLLoader.setDefaultClassLoader(this.getClass().getClassLoader());
URL location = KayakMain.class.getResource("path/to/my/resource.fxml");
Parent root = FXMLLoader.load(location);