java 通过servlet在java中将svg图像转换为png
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42340833/
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
Convert svg image to png in java by servlet
提问by Dinesh Pathak DK
Here, I am trying to send a SVG image to local server and in output I want to download that image in PNG / JPEG format.
在这里,我试图将 SVG 图像发送到本地服务器,并且在输出中我想以 PNG / JPEG 格式下载该图像。
While I have found some solutions but those are done by BATIK libraries, but in my Eclipse BATIK libraries are not supported , so I can't use the batik libraries.
虽然我找到了一些解决方案,但这些解决方案是由 BTIK 库完成的,但在我的 Eclipse BTIK 库中不受支持,因此我无法使用 batik 库。
回答by techhunter
Use batik library. Below is the code.
使用蜡染库。下面是代码。
import java.io.*;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import java.nio.file.Paths;
import java.nio.file.Path;
public class svg2png {
public static void main(String[] args) throws Exception {
//Step -1: We read the input SVG document into Transcoder Input
//We use Java NIO for this purpose
String svg_URI_input = Paths.get("chessboard.svg").toUri().toURL().toString();
TranscoderInput input_svg_image = new TranscoderInput(svg_URI_input);
//Step-2: Define OutputStream to PNG Image and attach to TranscoderOutput
OutputStream png_ostream = new FileOutputStream("chessboard.png");
TranscoderOutput output_png_image = new TranscoderOutput(png_ostream);
// Step-3: Create PNGTranscoder and define hints if required
PNGTranscoder my_converter = new PNGTranscoder();
// Step-4: Convert and Write output
my_converter.transcode(input_svg_image, output_png_image);
// Step 5- close / flush Output Stream
png_ostream.flush();
png_ostream.close();
}
}
Hope it will help you.
希望它会帮助你。
Refer this: http://thinktibits.blogspot.com/2012/12/Batik-Convert-SVG-PNG-Java-Program-Example.html
请参考:http: //thinktibits.blogspot.com/2012/12/Batik-Convert-SVG-PNG-Java-Program-Example.html
回答by Nupur Agarwal
You can also convert svg to png format without the use of Batik Transcoder. Follow below link: https://nupur28ag.blogspot.in/
您还可以在不使用蜡染转码器的情况下将 svg 转换为 png 格式。按照以下链接:https: //nupur28ag.blogspot.in/
BufferedImage input_image = null;
input_image = ImageIO.read(new File("Convert_to_PNG.svg")); //read svginto input_image object
File outputfile = new File("imageio_png_output.png"); //create new outputfile object
ImageIO.write(input_image, "PNG", outputfile);
By simply using the ImageIO
library. Hope this will help!
通过简单地使用ImageIO
库。希望这会有所帮助!