batik-users@xmlgraphics.apache.org
[Top] [All Lists]

RE: How to load an SVG resource into a java.awt.Image?

Subject: RE: How to load an SVG resource into a java.awt.Image?
From: Svenskmand
Date: Mon, 14 Jul 2008 17:23:34 -0700 PDT
Anybody knows if this class ever made it into Batik and if so under what
name?, if not then is there anybody that knows how to fix it so it works
with Batik 1.7, i get at compile error marked with the arrow /* >>> */
because there is missing a BridgeContext, but i don't know where to get it
from :S


Jon Johnson wrote:
> 
> Usages: java.awt.Image image = new SvgImage(url).getImage(50, 50);
>         java.awt.Image image = new SvgImage(doc).getImage(50, 50);
> 
> Code:
> 
> import org.apache.batik.bridge.BridgeContext;
> import org.apache.batik.bridge.GVTBuilder;
> import org.apache.batik.bridge.UserAgentAdapter;
> import org.apache.batik.bridge.ViewBox;
> import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
> import org.apache.batik.gvt.GraphicsNode;
> import org.apache.batik.util.XMLResourceDescriptor;
> import org.w3c.dom.Element;
> import org.w3c.dom.svg.SVGDocument;
> 
> import java.awt.*;
> import java.awt.geom.AffineTransform;
> import java.awt.image.BufferedImage;
> import java.io.IOException;
> import java.net.URL;
> 
> /**
>  * Immutable class to get the Image representation of a svg resource.
>  */
> public final class SvgImage
> {
>     /** Root node of svg document */
>     private final GraphicsNode rootSvgNode;
> 
>     /** Loaded SVG document */
>     private final SVGDocument svgDocument;
> 
>     /**
>      * Load the svg resource from a URL into a document.
>      * @param url location of svg resource.
>      * @throws java.io.IOException when svg resource cannot be read.
>      */
>     public SvgImage(URL url)
>         throws IOException
>     {
>         String parser = XMLResourceDescriptor.getXMLParserClassName();
>         SAXSVGDocumentFactory factory = new
> SAXSVGDocumentFactory(parser);
>         svgDocument =
> (SVGDocument)factory.createDocument(url.toString());
>         rootSvgNode = getRootNode(svgDocument);
>     }
> 
>     /**
>      * Load the svg from a document.
>      *
>      * @param document svg resource
>      */
>     public SvgImage(SVGDocument document)
>     {
>         svgDocument = document;
>         rootSvgNode = getRootNode(svgDocument);
>     }
> 
>     /**
>      * Get svg root from the given document.
>      *
>      * @param document svg resource
>      */
>     private static GraphicsNode getRootNode(SVGDocument document)
>     {
>         // Build the tree and get the document dimensions
>         UserAgentAdapter userAgentAdapter = new UserAgentAdapter();
>         BridgeContext bridgeContext = new
> BridgeContext(userAgentAdapter);
>         GVTBuilder builder = new GVTBuilder();
> 
>         return builder.build(bridgeContext, document);
>     }
> 
>     /**
>      * Get the svg root node of the document.
>      *
>      * @return svg root node.
>      */
>     public GraphicsNode getRootSvgNode()
>     {
>         return rootSvgNode;
>     }
> 
>     /**
>      * Get the svg document.
>      * @return the svg document.
>      */
>     public SVGDocument getSvgDocument()
>     {
>         return svgDocument;
>     }
> 
>     /**
>      * Renders and returns the svg based image.
>      *
>      * @param width desired width
>      * @param height desired height
>      * @return image of the rendered svg.
>      */
>     public Image getImage(int width, int height)
>     {
>         // Paint svg into image buffer
>         BufferedImage bufferedImage = new BufferedImage(width,
>             height, BufferedImage.TYPE_INT_ARGB);
>         Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
> 
>         // For a smooth graphic with no jagged edges or rastorized look.
>         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
>             RenderingHints.VALUE_ANTIALIAS_ON);
>         g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
>             RenderingHints.VALUE_INTERPOLATION_BILINEAR);
> 
>         // Scale image to desired size
>         Element elt = svgDocument.getRootElement();
>         /* >>> */AffineTransform usr2dev = ViewBox.getViewTransform(null,
> elt, width, height);
>         g2d.transform(usr2dev);
> 
>         rootSvgNode.paint(g2d);
> 
>         // Cleanup and return image
>         g2d.dispose();
>         return bufferedImage;
>     }
> }
> 

-- 
View this message in context: 
http://www.nabble.com/How-to-load-an-SVG-resource-into-a-java.awt.Image--tp2457141p18455900.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx
For additional commands, e-mail: batik-users-help@xxxxxxxxxxxxxxxxxxxxxx

<Prev in Thread] Current Thread [Next in Thread>