如何将 SVG 转换为 png 或 jpg [英] How to convert SVG to png or jpg
本文介绍了如何将 SVG 转换为 png 或 jpg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试过使用蜡染.但我得到了空的 png 文件.我还包括了所有必需的罐子.
I've tried by using batik. But I'm getting empty png file. I've also included all the required jars.
我的代码是
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
public class TestSVG {
String filePath="";
TestSVG(String filePath) throws Exception {
this.filePath=filePath;
createImage();
}
public void createImage() throws Exception{
String svg_URI_input = new File("test.svg").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
System.out.println("It will print");
my_converter.transcode(input_svg_image, output_png_image);
System.out.println("It will not print");
png_ostream.flush();
png_ostream.close();
}
}
请在代码中使用系统输出.到第 3 步,它工作正常.
Please the sysout in the code. Upto step 3 it is working fine.
推荐答案
有了你的代码,我就可以生成PNG图片了,
With your code, I can able to generate the PNG image,
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
public class TestSVG {
String filePath="";
TestSVG(String filePath) throws Exception {
this.filePath=filePath;
createImage();
}
public void createImage() throws Exception{
String svg_URI_input = new File("asf-logo.svg").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(filePath);
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
System.out.println("It will print");
my_converter.transcode(input_svg_image, output_png_image);
System.out.println("It will not print");
png_ostream.flush();
png_ostream.close();
}
public static void main(String[] args) throws Exception {
TestSVG svg = new TestSVG("asf-logo.png");
}
}
附加 生成的 png.
Attaching generated png.
这篇关于如何将 SVG 转换为 png 或 jpg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文