You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Kevin Finley <ho...@yahoo.com> on 2003/12/11 20:27:49 UTC

Encoding SVG files as PNG (and other formats)

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: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Encoding SVG files as PNG (and other formats)

Posted by Kevin Finley <ho...@yahoo.com>.
Thanks for your help Thomas.  I got everything I need working now.  :-D


__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: Encoding SVG files as PNG (and other formats)

Posted by Thomas DeWeese <Th...@Kodak.com>.
Kevin Finley wrote:

> 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):

    I believe that g.stream() or g.getRoot() 'clean out' the
SVGGraphics2D.  So only the stream to std out is correct everything
else is empty.  This isn't my area of expertise so it is possible I
am wrong.

    I don't know why Netscape says there are errors. I view PNG's
generated by Batik all the time in Netscape/Mozilla.  Note that
these PNG files do have an alpha channel - which not all viewers
understand.

>       Writer out = new OutputStreamWriter( System.out, "UTF-8" );
>       g.stream( out, useCSS );
>       Writer svgStringWriter = new StringWriter();
>       g.stream( svgStringWriter, useCSS );
>       svgString = svgStringWriter.toString();





---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org