use of org.apache.batik.util.Base64EncoderStream in project elki by elki-project.
the class CloneInlineImages method inlineThumbnail.
/**
* Inline a referenced thumbnail.
*
* @param doc Document (element factory)
* @param urldata URL
* @param eold Existing node
* @return Replacement node, or {@code null}
*/
protected Node inlineThumbnail(Document doc, ParsedURL urldata, Node eold) {
RenderableImage img = ThumbnailRegistryEntry.handleURL(urldata);
if (img == null) {
LoggingUtil.warning("Image not found in registry: " + urldata.toString());
return null;
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
os.write(SVGSyntax.DATA_PROTOCOL_PNG_PREFIX.getBytes());
Base64EncoderStream encoder = new Base64EncoderStream(os);
ImageIO.write(img.createDefaultRendering(), "png", encoder);
encoder.close();
} catch (IOException e) {
LoggingUtil.exception("Exception serializing image to png", e);
return null;
}
Element i = (Element) super.cloneNode(doc, eold);
i.setAttributeNS(SVGConstants.XLINK_NAMESPACE_URI, SVGConstants.XLINK_HREF_ATTRIBUTE, os.toString().replaceAll("\\s*[\\r\\n]+\\s*", ""));
return i;
}
use of org.apache.batik.util.Base64EncoderStream in project elki by elki-project.
the class CloneInlineImages method inlineExternal.
/**
* Inline an external file (usually from temp).
*
* @param doc Document (element factory)
* @param urldata URL
* @param eold Existing node
* @return Replacement node, or {@code null}
*/
protected Node inlineExternal(Document doc, ParsedURL urldata, Node eold) {
File in = new File(urldata.getPath());
if (!in.exists()) {
LoggingUtil.warning("Referencing non-existant file: " + urldata.toString());
return null;
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
os.write(SVGSyntax.DATA_PROTOCOL_PNG_PREFIX.getBytes());
Base64EncoderStream encoder = new Base64EncoderStream(os);
FileInputStream instream = new FileInputStream(in);
byte[] buf = new byte[4096];
while (true) {
int read = instream.read(buf, 0, buf.length);
if (read <= 0) {
break;
}
encoder.write(buf, 0, read);
}
instream.close();
encoder.close();
} catch (IOException e) {
LoggingUtil.exception("Exception serializing image to png", e);
return null;
}
Element i = (Element) super.cloneNode(doc, eold);
i.setAttributeNS(SVGConstants.XLINK_NAMESPACE_URI, SVGConstants.XLINK_HREF_ATTRIBUTE, os.toString().replaceAll("\\s*[\\r\\n]+\\s*", ""));
return i;
}
Aggregations