The following code prints correct-looking svg to stdout, but the resulting png
file is invalid (just a black rectangle in PaintShop Pro; Netscape says it
contains errors):
package imagingtest;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.dom.GenericDOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.svg.*;
import org.w3c.dom.DOMImplementation;
import org.apache.batik.svggen.*;
import org.apache.batik.transcoder.*;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.util.XMLResourceDescriptor;
public class ImagingTest
{
public ImagingTest()
{
}
public static void main( String[] args )
{
// Draw the image into an SVGGraphics2D.
// Get a DOMImplementation
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
// Create an instance of org.w3c.dom.Document
SVGDocument document = (SVGDocument)impl.createDocument(svgNS, "svg",
null);
// Create an instance of the SVG Generator
SVGGraphics2D g = new SVGGraphics2D(document);
g.setSVGCanvasSize( new Dimension( 200, 200 ) );
g.setBackground( Color.white );
g.setColor( Color.black );
// Draw the image into the generator.
g.drawLine( 0, 0, 200, 200 );
g.drawLine( 0, 200, 200, 0 );
// Write the image as svg to the screen.
boolean useCSS = true; // we want to use CSS style attribute
String svgString = "";
try
{
Writer out = new OutputStreamWriter( System.out, "UTF-8" );
g.stream( out, useCSS );
Writer svgStringWriter = new StringWriter();
g.stream( svgStringWriter, useCSS );
svgString = svgStringWriter.toString();
}
catch ( Exception ex )
{
ex.printStackTrace();
}
// Render the image to a png file.
try
{
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
Reader docReader = new StringReader( svgString );
Document doc = f.createDocument( null, docReader);
// Populate the document root with the generated SVG content.
Element root = document.getDocumentElement();
g.getRoot( root );
PNGTranscoder t = new PNGTranscoder();
TranscoderInput input = new TranscoderInput( doc );
OutputStream ostream = new FileOutputStream( "c:/Temp/out.png" );
TranscoderOutput output = new TranscoderOutput( ostream );
t.transcode( input, output );
ostream.flush();
ostream.close();
}
catch ( Exception ex1 )
{
ex1.printStackTrace();
}
}
}
What am I doing wrong?
Thanks in advance for any help,
Kevin Finley
__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]