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 Andres Toussaint <an...@onemileup.com> on 2005/02/13 20:21:31 UTC

Add IMAGE from BufferedImage into a loaded SVG Document

I want to add a thumbnail representation of my SVG document in the same 
document when a specific tool is selected, something like this:

1. Change "display" Atribute of TOOL handler layer to "inline" (to 
trigger a updateManager event)

2. Catch updateCompleted 
<http://xml.apache.org/batik/javadoc/org/apache/batik/bridge/UpdateManagerListener.html#updateCompleted%28org.apache.batik.bridge.UpdateManagerEvent%29>(UpdateManagerEvent 
<http://xml.apache.org/batik/javadoc/org/apache/batik/bridge/UpdateManagerEvent.html> e)

3. Get the thumbanil image by AffineTransform of the buffered image from 
updateManagerEvent.getImage()

4. Add the buffered image into my a SVG G Element (recuperated by a 
svgDoc.getElementByID("appendLocation")) in a <IMAGE> element. (This 
will be a added in the UpdateManager thread).

My Main question is: How is the best way to add this image into my document?

--------
Right now the only solution i have is the following, but i think there 
may be a simpler solution:
Create a new Document and a SVGGraphics2D object, and paint into it the 
BufferedImage, and then use the Document.importNode to add the thumbnail 
into the first document.

Also, will doing this approach create a Embedded Image in my SVG? Or how 
is the reference to the Image handled? What format of image will be 
embedded (PNG, JPG,...)?

Any comment will be greatly appreciated.

Andres.

Re: Add IMAGE from BufferedImage into a loaded SVG Document

Posted by Andres Toussaint <an...@onemileup.com>.
Thomas, thanks, this is a great simple approach. I will give it a try.

Just for the record, the approach solution indicated in the initial 
question does work also, but of course it is not  as simple.

Andres.

On Feb 14, 2005, at 10:59 AM, Thomas DeWeese wrote:

> Andres Toussaint wrote:
>
>> My Main question is: How is the best way to add this image into my 
>> document?
>
>     Encode it as a PNG and include it in the xlink:href using
> the data protocol.  This is what the code below does (very
> indirectly).
>
>     The code to do this more directly (take from
> batik.svggen.ImageHandlerBase64Encoder) is something like:
>
>     public static final String DATA_PROTOCOL_PNG_PREFIX =
> 	"data:image/png;base64,";
>
>         ByteArrayOutputStream os = new ByteArrayOutputStream();
>         Base64EncoderStream b64Encoder = new Base64EncoderStream(os);
>
>         ImageEncoder encoder = new PNGImageEncoder(b64Encoder, null);
>         encoder.encode(buf);
>
>         // Close the b64 encoder stream (terminates b64 streams).
>         b64Encoder.close();
>
>         imageElement.setAttributeNS(XLINK_NAMESPACE_URI,
>                                     ATTR_XLINK_HREF,
>                                     DATA_PROTOCOL_PNG_PREFIX +
>                                     os.toString());
>
>> Also, will doing this approach create a Embedded Image in my SVG? Or 
>> how is the reference to the Image handled? What format of image will 
>> be embedded (PNG, JPG,...)?
>
>    The above will embed the image in the SVG.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
>
>



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


Re: Add IMAGE from BufferedImage into a loaded SVG Document

Posted by Thomas DeWeese <Th...@Kodak.com>.
Andres Toussaint wrote:

> My Main question is: How is the best way to add this image into my document?

     Encode it as a PNG and include it in the xlink:href using
the data protocol.  This is what the code below does (very
indirectly).

     The code to do this more directly (take from
batik.svggen.ImageHandlerBase64Encoder) is something like:

     public static final String DATA_PROTOCOL_PNG_PREFIX =
	"data:image/png;base64,";

         ByteArrayOutputStream os = new ByteArrayOutputStream();
         Base64EncoderStream b64Encoder = new Base64EncoderStream(os);

         ImageEncoder encoder = new PNGImageEncoder(b64Encoder, null);
         encoder.encode(buf);

         // Close the b64 encoder stream (terminates b64 streams).
         b64Encoder.close();

         imageElement.setAttributeNS(XLINK_NAMESPACE_URI,
                                     ATTR_XLINK_HREF,
                                     DATA_PROTOCOL_PNG_PREFIX +
                                     os.toString());

> Also, will doing this approach create a Embedded Image in my SVG? Or how 
> is the reference to the Image handled? What format of image will be 
> embedded (PNG, JPG,...)?

    The above will embed the image in the SVG.

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