xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mukt <doshi_mukt...@yahoo.com>
Subject Problem: Getting node Image from SVG Image
Date Wed, 14 Nov 2007 15:37:43 GMT

I have written code to retrieve Image of a single node from entire image..the
code works fine.
The problem is it is too slow   
possibly due to following statement 
 builder.build(bridgeContext, thisDocument);

I am calling the getKeyBufferedImage() function several times and it becomes
very very slow.

My question is : Is there any need to call  builder.build(bridgeContext,
thisDocument); each time?
I am not able to get the result each time if I call the abve statement only
once(initially).
Also I have to initialize and reset the objects all the time.

Can anybody review the code and help me find the solution?
Also explain me..why I need to call builder.build each time?

<code>
  protected void initObjects()
    {
        userAgentAdapter = new UserAgentAdapter();
        bridgeContext = new BridgeContext(userAgentAdapter);
        builder = new GVTBuilder();
    }
    
    /**
     * Reset the objects. (What were you expecting?)
     */
    protected void resetObjects()
    {
        userAgentAdapter = null;
        bridgeContext = null;
        builder = null;
    }
public Image getKeyBufferedImage(final String keyString)
    {
        resetObjects();
        initObjects();
        
        GraphicsNode graphicsNode = null;


        builder.build(bridgeContext, thisDocument);
        
        try 
        {
            graphicsNode = builder.build(bridgeContext, 
                            thisDocument.getElementById(keyString));
        }
        catch (final Exception e)
        {
            logSVG.warning("Error creating graphics node. Key might be
absent. Key = " + keyString);
            e.printStackTrace();
            return null;
        }
            
        // Size up the rectangle in 2D space.
        final Rectangle2D bounds = graphicsNode.getBounds();

        final int margin = 3; 
        // Convert the 2D space into a rectangular region within the
document.
        final Rectangle areaOfInterest = new Rectangle((int) bounds.getX() -
1, 
                (int) bounds.getY() - 1, (int) bounds.getWidth() + margin, 
                (int) bounds.getHeight() + margin);
        
        
        //Assume that SVG is max of 1000 X 1000
        final int size1000 = 1000;
        Image imag = new BufferedImage(size1000, size1000,
BufferedImage.TYPE_4BYTE_ABGR);
        try 
        {
            //Get Image graphics
            final Graphics2D g = (Graphics2D) imag.getGraphics();
           
//g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
            //Because the SVG is rendered AntiAliased
            //Set same for current graphics object
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, //
Anti-alias!
                    RenderingHints.VALUE_ANTIALIAS_ON);
            //Pass the graphics object to node's paint method
            graphicsNode.paint((Graphics2D) g);

        }
        catch (final Exception e1) 
        {
            e1.printStackTrace();
        }
        //Crop the key Image..as it is painted at the actual location
        imag = createImage(new FilteredImageSource(imag.getSource(),
                new CropImageFilter(
                        areaOfInterest.x , areaOfInterest.y ,
areaOfInterest.width , areaOfInterest.height)));
        //return the resulting Image
        return imag;

    }
</code>
-- 
View this message in context: http://www.nabble.com/Problem%3A-Getting-node-Image-from-SVG-Image-tf4805947.html#a13749153
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Mime
View raw message