I am ingesting an AWT image (from PDF, through PDFBox) with the following code:
private java.awt.Graphics2D graphics;
public void drawImage(java.awt.Image awtImage, java.awt.geom.AffineTransform at) {
graphics.setComposite(getGraphicsState().getStrokeJavaComposite());
graphics.setClip(getGraphicsState().getCurrentClippingPath());
graphics.drawImage( awtImage, at, null );
}
and wish to capture/output the image as SVG. I have been using the Batik library which produces svg of the form
<image x="0" y="0" transform="matrix(0.144,0,0,0.1439,251.521,271.844)"
clip-path="url(#clipPath2)" width="1797" xlink:href="data:image/png;
base64,iVBORw0KGgoAAAANSUhEUgAABwUAAAV4CAMAAAB2DvLsAAADAFBMVEX////+/v56
enpWVlZbW1taWlpZWVnHx8eRkZFVVVWMjIysrKxXV1dYWFhqamr5+fnMzMxeXl7c
3NyUlJR/f3+3t7cAAACGhob29vYpKSliYmJPT083Nzf8/PyBgYENDQ3s7OwwMDD1
...
RERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERE
RPQP/R8CiIK+y8Q6KQAAAABJRU5ErkJggg==" height="1400"
preserveAspectRatio="none" stroke-width="0" xmlns:xlink="http://www.w3.org/1999/xlink"/>
I have my own SVG library and want to add methods to my SVGImage class to produce something similar. Do I need to apply the AffineTransformation to the data, and if so how?
I would be grateful to be pointed to appropriate (F/OSS) methods or libraries that can do this. The data should be inline (as in the above example) and XML-compliant.
UPDATE: In the absence of any answer or comments I have had to write my own implementation. This wasn’t completely trivial and may not be robust or the best solution. I append it as an answer – please comment if it’s flawed or could be improved.
[ANSWERING OWN QUESTION]
I assume the
java.awt.Imageis aBufferedImageas that has the appropriate methods. In the present case this has worked (so far).The SVG needs the MIME type of the image (e.g. “
image/png“).ImageIOneeds a type such as “PNG“.mimeType2ImageTypeMapmaps these.The Base64 codec is taken from Xerces but there are a lot around.
The DOM implementation is XOM and the attribute construction provides the namespace for xlink
The code above has worked on a test case so I am optimistic it will scale to large Images and other MIME types but haven’t tested them.
YES: I do have to apply the affine transformation. I already have utility routines for this which convert a 3*2 matrix of doubles to an SVG
transform="matrix(...)"attribute. So my final code is: