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.Image
is a BufferedImage
as 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
").
ImageIO
needs a type such as "PNG
".
mimeType2ImageTypeMap
maps 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
public class SVGImage extends xom.nu.Element {
private static final String DATA = "data";
private static final String BASE64 = "base64";
public static final String IMAGE_PNG = "image/png";
public static final String PNG = "PNG";
private static final String XLINK_PREF = "xlink";
private static final String HREF = "href";
private static final String XLINK_NS = "http://www.w3.org/1999/xlink";
public void readImageData(BufferedImage bufferedImage, String mimeType) {
String type = mimeType2ImageTypeMap.get(mimeType);
if (type == null) {
throw new RuntimeException("Cannot convert mimeType: "+mimeType);
}
double x = bufferedImage.getMinX();
double y = bufferedImage.getMinY();
double height = bufferedImage.getHeight();
double width = bufferedImage.getWidth();
this.setX(x);
this.setY(y);
this.setWidth(width);
this.setHeight(height);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(bufferedImage, type, baos);
} catch (IOException e) {
throw new RuntimeException("Cannot read image", e);
}
byte[] byteArray = baos.toByteArray();
String base64 = Base64.encode(byteArray);
String attValue = DATA+":"+mimeType+";"+BASE64+","+base64;
this.addAttribute(new Attribute(XLINK_PREF+":"+HREF, XLINK_NS, attValue));
}
}
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:
Transform2 t2 = new Transform2(at); // converts matrix syntax
BufferedImage bImage = (BufferedImage) awtImage;
SVGImage svgImage = new SVGImage();
svgImage.setTransform(t2);
svgImage.readImageData(bImage, SVGImage.IMAGE_PNG);