简体   繁体   English

使用 Apache Batik 从 SVG 生成 JPG

[英]Generate JPG from SVG using Apache Batik

I'm trying to turn a .svg file into a .jpg or .png file.我正在尝试将.svg文件转换为.jpg.png文件。 Right now I'm trying to use Apache Batik Transcoder ( Link ).现在我正在尝试使用Apache Batik Transcoder ( Link )。

This is my code right now: (official code from here )这是我现在的代码:(来自此处的官方代码)

// Create a JPEG transcoder
JPEGTranscoder t = new JPEGTranscoder();

// Set the transcoding hints.
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
    new Float(.8));

// Create the transcoder input.
String svgURI = new File("C:/test.svg").toURL().toString();
TranscoderInput input = new TranscoderInput(svgURI);

// Create the transcoder output.
OutputStream ostream = new FileOutputStream("C:/out.jpg");
TranscoderOutput output = new TranscoderOutput(ostream);

// Save the image.
t.transcode(input, output);

// Flush and close the stream.
ostream.flush();
ostream.close();
System.exit(0);

But it gives my this error:但它给了我这个错误:

Exception in thread "main" org.apache.batik.transcoder.TranscoderException: null线程“主”org.apache.batik.transcoder.TranscoderException 中的异常:null

Enclosed Exception:封闭式异常:

null

at org.apache.batik.transcoder.image.ImageTranscoder.transcode(Unknown Source)在 org.apache.batik.transcoder.image.ImageTranscoder.transcode(来源不明)

at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown Source)在 org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(来源不明)

at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source)在 org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(来源不明)

at SaveAsJPEG.main(SaveAsJPEG.java:27)在 SaveAsJPEG.main(SaveAsJPEG.java:27)

I don't know what to do as this is the code from their website (!).我不知道该怎么做,因为这是他们网站上的代码(!)。 So if you know how to solve it or have another idea how I can turn my .svg pictures into .jpg and .png's let me know.因此,如果您知道如何解决它或有其他想法,我可以如何将我的 .svg 图片转换为 .jpg 和 .png,请告诉我。

Well, I have now found the solution to my problem.好吧,我现在找到了解决我的问题的方法。 It seems like Batik 1.8 (which I have used) does not work properly (or at least does not work in the way they say it on their API page.似乎Batik 1.8 (我使用过)不能正常工作(或者至少不能像他们在 API 页面上所说的那样工作。

I now used Batik 1.7 wich works just fine.我现在使用Batik 1.7,效果很好。 So now everything is fine and to everyone who wants to use Batik: I suggest you to use 1.7 because the documentation is not about 1.8.所以现在一切都很好,对于想要使用 Batik 的每个人:我建议您使用 1.7,因为文档不是关于 1.8。

如果您将batik-codec添加到maven而不是transcoder这可以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM