Search in sources :

Example 1 with Base64EncoderStream

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;
}
Also used : RenderableImage(java.awt.image.renderable.RenderableImage) Element(org.w3c.dom.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Base64EncoderStream(org.apache.batik.util.Base64EncoderStream) IOException(java.io.IOException)

Example 2 with Base64EncoderStream

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;
}
Also used : Element(org.w3c.dom.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Base64EncoderStream(org.apache.batik.util.Base64EncoderStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 Base64EncoderStream (org.apache.batik.util.Base64EncoderStream)2 Element (org.w3c.dom.Element)2 RenderableImage (java.awt.image.renderable.RenderableImage)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1