[prev in list] [next in list] [prev in thread] [next in thread]
List: batik-dev
Subject: Re: documentation is a terrible thing to waste :-)
From: Mariano Kamp <mkamp () codamax ! com>
Date: 2001-02-18 18:45:49
[Download RAW message or body]
On Sunday 11 February 2001 00:14 Mariano Kamp wrote:
> > > A small price to pay though for
> > > documentation. Is there any specific need that you see for the
> > > documentation to address?
> >
> > I think that a developer's Q&A would be good, i.e., questions like,
> > "How do I create a JPEG or PNG image from an SVG Document" with
> > an answer showing a code snippet, or "What is the bridge?" with a
> > short answer on the module's high level responsibility.
> > Any other suggestions?
>
> I guess meanwhile I should be qualified to provide the base for the answer
> of the first question. The information is on the list already.
> I'll do that on next weeks sunday and post it back to the list. Ryan,
> maybe you can incorporate it?!
Well, my initial guess was wrong. I am not qualified to build the base for
the first question. There are too many black holes for me.
I attached the starting point of my "question" to this mail, but it's
rubbish. I have to go into it much deeper to understand what all the classes
are doing and right now I can't do that. I am currently working on Sundays
for my regular job. I probably resume this later on.
Sorry,
Mariano
--
mariano kamp
codamax ag
hessenring 89
61348 bad homburg v.d.h.
germany
phone +49 6172 182 473
fax +49 6172 182 475
mobile +49 173 300 8564 (changed!)
["QA.html" (text/html)]
<h1>How do I create a JPEG or PNG image from a SVG document?</h1>
<p>Converting an SVG document to an image is referred to as transcoding.</p>
<p>All Transcoders are implementing the \
<code><b>org.apache.batik.transcoder.Transcoder</b></code> interface. The Transcoder \
interface defines the <code><b>transcode(TranscoderInput input, TranscoderOutput \
output)</b></code> method. TranscoderInput is used to wrap different means of input \
sources in a uniform way. In the below mentioned sample code a Document will be \
passed to the transcode method. TranscoderOutput is the matching counterpart and will \
be used to wrap an OutputStream in the below mentioned sample code.</p> \
<p>Furthermore <code><b>org.apache.batik.transcoder.Transcoder</b></code> defines \
methods for supplying hints to the transcoding process.<br>General hints can be \
applied like <b><code>XMLAbstractTranscoder.KEY_XML_PARSER_CLASSNAME</b></code> and \
more specialised hints like <b><code>JPEGTranscoder.KEY_QUALITY</b></code>.<br> With \
<b><code>ImageTranscoder.KEY_HEIGHT</b></code> and \
<b><code>ImageTranscoder.KEY_WIDTH</b></code> the height and width of the resulting \
image could probably be hinted, but I wasn't able to get this working:<br>Exception \
in thread "main" java.lang.NullPointerException<br>
at org.apache.batik.bridge.DefaultUnitProcessorContext.getViewportWidth(DefaultUnitProcessorContext.java:69)<br>
at org.apache.batik.util.UnitProcessor.percentagesToPixels(UnitProcessor.java:459)<br>
at org.apache.batik.util.UnitProcessor.svgToUserSpace(UnitProcessor.java:177)<br>
at org.apache.batik.bridge.SVGUtilities.svgToUserSpace(SVGUtilities.java:1173)<br>
at org.apache.batik.bridge.SVGSVGElementBridge.createGraphicsNode(SVGSVGElementBridge.java:88)<br>
at org.apache.batik.bridge.ConcreteGVTBuilder.build(ConcreteGVTBuilder.java:85)<br>
at org.apache.batik.transcoder.image.ImageTranscoder.transcode(ImageTranscoder.java:174)<br>
at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:122)<br>
at JavaToSvgToJpeg.main(JavaToSvgToJpeg.java:92)<br><br>
Instead height and width can be specified in the Document as can be seen in the below \
mentioned sample code. </p>
<p>Currently batik comes with two concrete implementations for image transcoders:<br>
<ul>
<li>
<code><b>org.apache.batik.transcoder.image.JPEGTranscoder</b></code>
</li>
<li>
<code><b>org.apache.batik.transcoder.image.PNGTranscoder</b></code>
</li>
</ul>
</p>
<hr>
<code>
/*****************************************************************************<br>
* Copyright (C) The Apache Software Foundation.&nb \
sp;All rights reserved. *<br>
* ------------------------------------------------------------------------- *<br>
* This software is published under the terms of the Apache Software License *<br>
* version 1.1, a copy of which has been included with this distribution in *<br>
* the LICENSE file. &nb \
sp;   \
; & \
nbsp; *<br>
*****************************************************************************/<br>
<br>
import org.apache.batik.dom.svg.SVGDOMImplementation;<br>
import org.apache.batik.svggen.SVGGraphics2D;<br>
import org.apache.batik.transcoder.image.JPEGTranscoder;<br>
import org.apache.batik.transcoder.TranscoderInput;<br>
import org.apache.batik.transcoder.TranscoderOutput;<br>
import org.apache.batik.transcoder.TranscoderException;<br>
<br>
import org.w3c.dom.Document;<br>
import org.w3c.dom.Element;<br>
import org.w3c.dom.DOMImplementation;<br>
<br>
import java.awt.Color;<br>
import java.awt.Dimension;<br>
import java.io.File;<br>
import java.io.FileOutputStream;<br>
import java.io.OutputStreamWriter;<br>
<br>
/**<br>
* An example showing how to:<br>
* a) Use SVGGraphics2D to generate a DOM tree<br>
* b) Use the generated DOM tree as an input to a transcoder, JPEG<br>
* in this example.<br>
*<br>
* @author <a href="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a><br>
* @version $Id: JavaToSvgToJpeg.java,v 1.2 2001/02/07 18:34:51 mkamp Exp $<br>
*/<br>
public class JavaToSvgToJpeg {<br>
public static final String USAGE = "java JavaToSvgJpeg <outputJpegFileName>";<br>
<br>
public static void main(String args[]) throws Exception{<br>
if(args.length < 1){<br>
System.err.println(USAGE);<br>
System.exit(0);<br>
}<br>
<br>
//<br>
// Build SVGGraphics2D with Batik's DOM implementation<br>
//<br>
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();<br>
String namespaceURI = SVGDOMImplementation.SVG_NAMESPACE_URI;<br>
Document doc = impl.createDocument(namespaceURI, "svg", null);<br>
SVGGraphics2D g = new SVGGraphics2D(doc);<br>
<br>
//<br>
// Draw something<br>
//<br>
g.setPaint(Color.black);<br>
g.fillRect(0, 0, 300, 300);<br>
g.setPaint(Color.white);<br>
g.fillOval(20, 20, 260, 260);<br>
//<br>
// Set document size<br>
//<br>
<b>doc.getDocumentElement().setAttributeNS(null, "width", "300");<br>
doc.getDocumentElement().setAttributeNS(null, "height", "300");</b><br>
<br>
//<br>
// Set document content<br>
//<br>
doc.getDocumentElement().appendChild(g.getRoot());<br>
<br>
//<br>
// Convert to JPEG<br>
//<br>
<b>TranscoderInput svgInput = new TranscoderInput(doc);<br>
JPEGTranscoder jpegTranscoder = new JPEGTranscoder();<br>
jpegTranscoder.addTranscodingHint(JPEGTranscoder.KEY_XML_PARSER_CLASSNAME,<br>
& \
nbsp; &nb \
sp; "org.apache.crimson.parser.XMLReaderImpl");<br>
jpegTranscoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,<br>
& \
nbsp; &nb \
sp; new Float(1));<br>
// transcode image<br>
File imageFile = new File(args[0]);<br>
TranscoderOutput imageOutput = new TranscoderOutput(new FileOutputStream(imageFile));<br>
jpegTranscoder.transcode(svgInput, imageOutput);<br>
</b>}<br>
}<br>
</code>
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic