【问题标题】:Convert SVG written in HTML5 to JPG using JAVA使用 JAVA 将 HTML5 编写的 SVG 转换为 JPG
【发布时间】:2018-05-11 17:10:36
【问题描述】:

是否可以使用 Java 将纯 SVG 代码转换为 JPG?我尝试使用 batik SVG,但它需要文件路径才能进行对话,我只想使用代码。

SVG 代码:

String chart = "<svg width=\"700\" height=\"297\" aria-label=\"A chart.\" style=\"overflow: hidden;\"><defs id=\"defs\"><clipPath id=\"_ABSTRACT_RENDERER_ID_0\"><rect x=\"175\" y=\"57\" width=\"350\" height=\"184\"></rect></clipPath><filter id=\"_ABSTRACT_RENDERER_ID_1\"><feGaussianBlur in=\"SourceAlpha\" stdDeviation=\"2\"></feGaussianBlur><feOffset dx=\"1\" dy=\"1\"></feOffset><feComponentTransfer><feFuncA type=\"linear\" slope=\"0.1\"></feFuncA></feComponentTransfer><feMerge><feMergeNode></feMergeNode><feMergeNode in=\"SourceGraphic\"></feMergeNode></feMerge></filter></defs><rect x=\"0\" y=\"0\" width=\"700\" height=\"297\" stroke=\"none\" stroke-width=\"0\" fill=\"#ffffff\"></rect><g><rect x=\"538\" y=\"57\" width=\"149\" height=\"13\" stroke=\"none\" stroke-width=\"0\" fill-opacity=\"0\" fill=\"#ffffff\"></rect><g><rect x=\"538\" y=\"57\" width=\"149\" height=\"13\" stroke=\"none\" stroke-width=\"0\" fill-opacity=\"0\" fill=\"#ffffff\"></rect><g><text text-anchor=\"start\" x=\"569\" y=\"68.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">Wynik</text></g><rect x=\"538\" y=\"57\" width=\"26\" height=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#3366cc\"></rect></g></g><g><rect x=\"175\" y=\"57\" width=\"350\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill-opacity=\"0\" fill=\"#ffffff\"></rect><g clip-path=\"url(http://localhost:8080/summary#_ABSTRACT_RENDERER_ID_0)\"><g><rect x=\"175\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect><rect x=\"262\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect><rect x=\"350\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect><rect x=\"437\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect><rect x=\"524\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#cccccc\"></rect></g><g><rect x=\"176\" y=\"64\" width=\""+  String.valueOf(getGroupAnswer.get(0).getValue() * 5.79) +"\" height=\"23\" stroke=\"#ffa500\" stroke-width=\"1\" fill=\"#ffa500\"></rect><rect x=\"176\" y=\"101\" width=\""+  String.valueOf(getGroupAnswer.get(1).getValue() * 5.79) +"\" height=\"23\" stroke=\"#b87333\" stroke-width=\"1\" fill=\"#b87333\"></rect><rect x=\"176\" y=\"138\" width=\""+  String.valueOf(getGroupAnswer.get(2).getValue() * 5.79) +"\" height=\"22\" stroke=\"#c0c0c0\" stroke-width=\"1\" fill=\"#c0c0c0\"></rect><rect x=\"176\" y=\"174\" width=\""+  String.valueOf(getGroupAnswer.get(3).getValue() * 5.79) +"\" height=\"23\" stroke=\"#ffd700\" stroke-width=\"1\" fill=\"#ffd700\"></rect><rect x=\"176\" y=\"211\" width=\""+  String.valueOf(getGroupAnswer.get(4).getValue() * 5.79) +"\" height=\"23\" stroke=\"#e5e4e2\" stroke-width=\"1\" fill=\"#e5e4e2\"></rect></g><g><rect x=\"175\" y=\"57\" width=\"1\" height=\"184\" stroke=\"none\" stroke-width=\"0\" fill=\"#333333\"></rect></g></g><g></g><g><g><text text-anchor=\"middle\" x=\"175.5\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">0</text></g><g><text text-anchor=\"middle\" x=\"262.75\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">15</text></g><g><text text-anchor=\"middle\" x=\"350\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">30</text></g><g><text text-anchor=\"middle\" x=\"437.25\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">45</text></g><g><text text-anchor=\"middle\" x=\"524.5\" y=\"260.05\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#444444\">60</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"80.35\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST4</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"116.95\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST1</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"153.55\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST5</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"190.15\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST3</text></g><g><text text-anchor=\"end\" x=\"162\" y=\"226.75000000000003\" font-family=\"Arial\" font-size=\"13\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">TEST2</text></g></g></g><g><g><text text-anchor=\"middle\" x=\"350\" y=\"284.4562\" font-family=\"Arial\" font-size=\"13\" font-style=\"italic\" stroke=\"none\" stroke-width=\"0\" fill=\"#222222\">Wynik całkowity</text><rect x=\"175\" y=\"273.4062\" width=\"350\" height=\"13\" stroke=\"none\" stroke-width=\"0\" fill-opacity=\"0\" fill=\"#ffffff\"></rect></g></g><g></g></svg>";

【问题讨论】:

标签: java spring html svg thymeleaf


【解决方案1】:

你可以将String转换成InputStream:

String chart = "<svg width=\"700\...";
InputStream stream = new ByteArrayInputStream(chart.getBytes(StandardCharsets.UTF_8.name()));

并将其传递给 TranscoderInput 构造函数:

TranscoderInput input = new TranscoderInput(stream); 

【讨论】:

  • 转码器输入?抱歉,我对 Java 和这个库不太熟悉 - 你能解释更多吗?
  • 您说您使用蜡染将 SVG 转换为 jpg。 TranscoderInput 是要转换的输入 (SVG) 的对象。你可以找到一个完整的例子java2s.com/Code/Java/XML/SVGtoJPEG.htm
  • 因此,您可以使用 new ByteArrayInputStream(chart.getBytes(StandardCharsets.UTF_8.name())) 代替示例中的 new FileInputStream("rectangles.svg") 从字符串中获取 SVG 数据.svg 文件。
猜你喜欢
  • 2013-12-29
  • 2012-12-19
  • 2012-04-09
  • 1970-01-01
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
  • 2015-10-06
  • 2013-03-07
相关资源
最近更新 更多