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 ali can <al...@hotmail.com> on 2012/09/07 10:39:16 UTC

How to render embedded png?

I have an SVG file with an embed image(data:image protocol, png format). I am
not able to find how to render this SVG file correctly. Is there a simple
example/tutorial on how to render embed images using batik? 



--
View this message in context: http://batik.2283329.n4.nabble.com/How-to-render-embedded-png-tp4655236.html
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


Re: How to render embedded png?

Posted by jonathan wood <jo...@gmail.com>.
Great news!  I have to admit that after I reproduced the problem, I was a
bit stumped.  Nice work

jonathan

On Fri, Sep 14, 2012 at 12:10 PM, ali can <al...@hotmail.com> wrote:

> The problem is solved. For the sake of others who may encounter it, the
> culprit was missing batik-codec.jar and js.jar. When they were added, all
> started to work as expected.
> Thanks, Thomas and Jonathan
>
>
>
> --
> View this message in context:
> http://batik.2283329.n4.nabble.com/How-to-render-embedded-png-tp4655236p4655251.html
> 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
>
>

Re: How to render embedded png?

Posted by ali can <al...@hotmail.com>.
The problem is solved. For the sake of others who may encounter it, the
culprit was missing batik-codec.jar and js.jar. When they were added, all
started to work as expected.
Thanks, Thomas and Jonathan



--
View this message in context: http://batik.2283329.n4.nabble.com/How-to-render-embedded-png-tp4655236p4655251.html
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


Re: How to render embedded png?

Posted by ali can <al...@hotmail.com>.
I am using a file from the Batik Sample folder, "embedData.svg" and still
don't see the embed png.
Below is my whole program. Do you spot any problem? Can it be DOM/SAX
differences? Should I use DOM?
Is there any import missed?
Thanks a lot, the thing is driving me crazy, hope somebody can find
something.
 
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.util.List;

import javax.swing.*;

import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.BridgeException;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.dom.AbstractParentNode;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.gvt.CanvasGraphicsNode;
import org.apache.batik.gvt.CompositeGraphicsNode;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.gvt.renderer.ConcreteImageRendererFactory;
import org.apache.batik.gvt.renderer.ImageRenderer;
import org.apache.batik.gvt.renderer.ImageRendererFactory;
import org.apache.batik.svggen.DefaultImageHandler;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.util.Base64EncoderStream;
import org.apache.batik.util.XMLConstants;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * A applet
 *
 * @version $Id$
 */

public class EasyTest extends JApplet {

    protected JSVGCanvas canvas;
    protected Document doc;

    public void init() {
        // Create a new JSVGCanvas.
        canvas = new JSVGCanvas();
        JPanel svgPanel = new JPanel();
        svgPanel.add(canvas);
        getContentPane(). setLayout(new FlowLayout());
        getContentPane().add(svgPanel);

        try {
            // Parse the svg file into a Document.
            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
            URL url = new URL(getCodeBase(), "embedData.svg");
            doc = f.createDocument(url.toString());

        } catch (Exception ex) {
        }
    }

    public void start() {
        // Display the document.
        canvas.setDocumentState(JSVGCanvas.AUTODETECT);
        canvas.setDocument(doc);
    }

    public void stop() {
        // Remove the document.
        canvas.setDocument(null);
    }

    public void destroy() {
        canvas.dispose();
    }

}



--
View this message in context: http://batik.2283329.n4.nabble.com/How-to-render-embedded-png-tp4655236p4655248.html
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


Re: How to render embedded png?

Posted by jonathan wood <jo...@gmail.com>.
You may be accessing the document before the gvt builder is complete... try
adding a listener and then loading the doc. Begin your work when the
listener is called...


        canvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
            @Override
            public void gvtBuildCompleted(GVTTreeBuilderEvent e) {

              // do stuff here

            }
        });

On Wed, Sep 12, 2012 at 6:45 PM, Thomas DeWeese
<th...@gmail.com> wrote:

Hi Ali,
>         Hmm, I think we misunderstood.  You have an SVG document and you
> can view most of it in the JSVGCanvas but the embedded png's don't show up.
> In that case I think you need to send a sample document that shows the
> problem for you because they should just show up (there are examples
> included in batik like 'sample/tests/spec/structure/dataProtocol.svg'.
>
>         Thomas
>
> On Sep 12, 2012, at 12:11 PM, ali can wrote:
>
> > Please excuse my ignorance but I really cannot get a grip of this. Here
> is my
> > code, I can see svg, but no embed image. What is wrong?
> > Thanks
> >
> >        canvas = new JSVGCanvas();
> >        getContentPane().add(canvas);
> >
> >        userAgent = new UserAgentAdapter();
> >        ctx       = new BridgeContext(userAgent);
> >        builder   = new GVTBuilder();
> >        try {
> >            // Parse the barChart.svg file into a Document.
> >            String parser = XMLResourceDescriptor.getXMLParserClassName();
> >            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
> >            URL url = new URL(getCodeBase(), "embedData.svg");
> >            doc = f.createDocument(url.toString());
> >
> >            svg = doc.getDocumentElement();
> >
> >
> >            NodeList l =
> >            svg.getElementsByTagName("image")     ;
> >            for (int i = 0; i < l.getLength(); i++)
> >            {
> >                Element n = (Element)l.item(i)   ;
> >                String image =
> >                        n.getAttributeNS(XMLConstants.XLINK_NAMESPACE_URI,
> > "href");
> >                String t = n.getAttributeNS(null, SVG_WIDTH_ATTRIBUTE);
> >                int width = Integer.parseInt(t);
> >                int height = Integer.parseInt(n.getAttributeNS(null,
> > SVG_HEIGHT_ATTRIBUTE));
> >                String id = "image" + Integer.toString(i);
> >
> >                // Create and initialize the new image element
> >                Element imageElement =
> > doc.createElementNS(SVG_NAMESPACE_URI,
> >                        SVG_IMAGE_TAG);
> >                imageElement.setAttributeNS(null,
> > XMLConstants.XML_ID_ATTRIBUTE,
> >                        id);
> >                imageElement.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE,
> >                        Integer.toString(width));
> >                imageElement.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE,
> >                        Integer.toString(height));
> >
> > imageElement.setAttributeNS(XMLConstants.XLINK_NAMESPACE_URI,
> > XMLConstants.XLINK_HREF_QNAME,
> >                        "data::image/PNG;base64," +
> > base64Encode(image.getBytes()));
> >                doc.appendChild(imageElement);
> >                svg.removeChild(n);
> >            }
> >        } catch (Exception ex) {
> >        }
> >
> >
> >
> >
> >
> > --
> > View this message in context:
> http://batik.2283329.n4.nabble.com/How-to-render-embedded-png-tp4655236p4655244.html
> > 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
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>

Re: How to render embedded png?

Posted by Thomas DeWeese <th...@gmail.com>.
Hi Ali,
	Hmm, I think we misunderstood.  You have an SVG document and you can view most of it in the JSVGCanvas but the embedded png's don't show up.
In that case I think you need to send a sample document that shows the problem for you because they should just show up (there are examples
included in batik like 'sample/tests/spec/structure/dataProtocol.svg'.

	Thomas

On Sep 12, 2012, at 12:11 PM, ali can wrote:

> Please excuse my ignorance but I really cannot get a grip of this. Here is my
> code, I can see svg, but no embed image. What is wrong? 
> Thanks
> 
>        canvas = new JSVGCanvas();
>        getContentPane().add(canvas);
> 
>        userAgent = new UserAgentAdapter();
>        ctx       = new BridgeContext(userAgent);
>        builder   = new GVTBuilder();
>        try {
>            // Parse the barChart.svg file into a Document.
>            String parser = XMLResourceDescriptor.getXMLParserClassName();
>            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
>            URL url = new URL(getCodeBase(), "embedData.svg");
>            doc = f.createDocument(url.toString());
> 
>            svg = doc.getDocumentElement();
> 
> 
>            NodeList l =
>            svg.getElementsByTagName("image")     ;
>            for (int i = 0; i < l.getLength(); i++)
>            {
>                Element n = (Element)l.item(i)   ;
>                String image =
>                        n.getAttributeNS(XMLConstants.XLINK_NAMESPACE_URI,
> "href");
>                String t = n.getAttributeNS(null, SVG_WIDTH_ATTRIBUTE);
>                int width = Integer.parseInt(t);
>                int height = Integer.parseInt(n.getAttributeNS(null,
> SVG_HEIGHT_ATTRIBUTE));
>                String id = "image" + Integer.toString(i);
> 
>                // Create and initialize the new image element
>                Element imageElement =
> doc.createElementNS(SVG_NAMESPACE_URI,
>                        SVG_IMAGE_TAG);
>                imageElement.setAttributeNS(null,
> XMLConstants.XML_ID_ATTRIBUTE,
>                        id);
>                imageElement.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE,
>                        Integer.toString(width));
>                imageElement.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE,
>                        Integer.toString(height));
> 
> imageElement.setAttributeNS(XMLConstants.XLINK_NAMESPACE_URI,
> XMLConstants.XLINK_HREF_QNAME,
>                        "data::image/PNG;base64," +
> base64Encode(image.getBytes()));
>                doc.appendChild(imageElement);
>                svg.removeChild(n);
>            }
>        } catch (Exception ex) {
>        }
> 
> 
> 
> 
> 
> --
> View this message in context: http://batik.2283329.n4.nabble.com/How-to-render-embedded-png-tp4655236p4655244.html
> 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
> 


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


Re: How to render embedded png?

Posted by ali can <al...@hotmail.com>.
Please excuse my ignorance but I really cannot get a grip of this. Here is my
code, I can see svg, but no embed image. What is wrong? 
Thanks

        canvas = new JSVGCanvas();
        getContentPane().add(canvas);

        userAgent = new UserAgentAdapter();
        ctx       = new BridgeContext(userAgent);
        builder   = new GVTBuilder();
        try {
            // Parse the barChart.svg file into a Document.
            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
            URL url = new URL(getCodeBase(), "embedData.svg");
            doc = f.createDocument(url.toString());

            svg = doc.getDocumentElement();


            NodeList l =
            svg.getElementsByTagName("image")     ;
            for (int i = 0; i < l.getLength(); i++)
            {
                Element n = (Element)l.item(i)   ;
                String image =
                        n.getAttributeNS(XMLConstants.XLINK_NAMESPACE_URI,
"href");
                String t = n.getAttributeNS(null, SVG_WIDTH_ATTRIBUTE);
                int width = Integer.parseInt(t);
                int height = Integer.parseInt(n.getAttributeNS(null,
SVG_HEIGHT_ATTRIBUTE));
                String id = "image" + Integer.toString(i);

                // Create and initialize the new image element
                Element imageElement =
doc.createElementNS(SVG_NAMESPACE_URI,
                        SVG_IMAGE_TAG);
                imageElement.setAttributeNS(null,
XMLConstants.XML_ID_ATTRIBUTE,
                        id);
                imageElement.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE,
                        Integer.toString(width));
                imageElement.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE,
                        Integer.toString(height));
               
imageElement.setAttributeNS(XMLConstants.XLINK_NAMESPACE_URI,
XMLConstants.XLINK_HREF_QNAME,
                        "data::image/PNG;base64," +
base64Encode(image.getBytes()));
                doc.appendChild(imageElement);
                svg.removeChild(n);
            }
        } catch (Exception ex) {
        }





--
View this message in context: http://batik.2283329.n4.nabble.com/How-to-render-embedded-png-tp4655236p4655244.html
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


Re: How to render embedded png?

Posted by Thomas DeWeese <th...@gmail.com>.
You need to construct an SVG 'image' element (createElementNS) and set the
base64 encoded byte array as the xlink:href (setAttributeNS) (and set other
attributes as needed such as x, y, width, height) then insert the image
element into the SVG Document (appendChild).
Make sure you only modify the document in the UpdateManager's RunnableQueue.

Thomas

On Mon, Sep 10, 2012 at 3:23 AM, ali can <al...@hotmail.com> wrote:

> Thanks a lot, Jonathan, but getting this byte array, how I draw it on the
> SVG
> canvas?
> Is there any batik API or should I use the standard Java means?
> Thanks again.
> AC
>
>
>
>
> --
> View this message in context:
> http://batik.2283329.n4.nabble.com/How-to-render-embedded-png-tp4655236p4655240.html
> 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
>
>

Re: How to render embedded png?

Posted by ali can <al...@hotmail.com>.
Thanks a lot, Jonathan, but getting this byte array, how I draw it on the SVG
canvas?
Is there any batik API or should I use the standard Java means?
Thanks again.
AC




--
View this message in context: http://batik.2283329.n4.nabble.com/How-to-render-embedded-png-tp4655236p4655240.html
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


Re: How to render embedded png?

Posted by jonathan wood <jo...@gmail.com>.
I've had success using the following:

http://xmlgraphics.apache.org/batik/javadoc/org/apache/batik/util/Base64EncoderStream.html

snippet that I use:

...
element.setAttributeNS(SVGConstants.XLINK_NAMESPACE_URI,
SVGConstants.XLINK_HREF_QNAME,
                                                "data:;base64," +
base64Encode(myimagebytearray);

...

    private String base64Encode(final byte[] data) {
        ByteArrayOutputStream b64out = new ByteArrayOutputStream();
        Base64EncoderStream enc = new Base64EncoderStream(b64out);
        try {
            enc.write(data);
        } catch (IOException ex) {
            logger.error(null, ex);
        } finally {
            if (null != enc) {
                try {
                    enc.close();
                } catch (IOException ex) {
                    logger.error(null, ex);
                }
            }
        }
        return b64out.toString();
    }


Hope this helps


On Fri, Sep 7, 2012 at 4:39 AM, ali can <al...@hotmail.com> wrote:

> I have an SVG file with an embed image(data:image protocol, png format). I
> am
> not able to find how to render this SVG file correctly. Is there a simple
> example/tutorial on how to render embed images using batik?
>
>
>
> --
> View this message in context:
> http://batik.2283329.n4.nabble.com/How-to-render-embedded-png-tp4655236.html
> 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
>
>