Example usage for javax.imageio.metadata IIOMetadata mergeTree

List of usage examples for javax.imageio.metadata IIOMetadata mergeTree

Introduction

In this page you can find the example usage for javax.imageio.metadata IIOMetadata mergeTree.

Prototype

public abstract void mergeTree(String formatName, Node root) throws IIOInvalidTreeException;

Source Link

Document

Alters the internal state of this IIOMetadata object from a tree of XML DOM Node s whose syntax is defined by the given metadata format.

Usage

From source file:com.ackpdfbox.app.imageio.ImageIOUtil.java

private static void setDPI(IIOMetadata metadata, int dpi, String formatName) throws IIOInvalidTreeException {
    IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(MetaUtil.STANDARD_METADATA_FORMAT);

    IIOMetadataNode dimension = getOrCreateChildNode(root, "Dimension");

    // PNG writer doesn't conform to the spec which is
    // "The width of a pixel, in millimeters"
    // but instead counts the pixels per millimeter
    float res = "PNG".equals(formatName.toUpperCase()) ? dpi / 25.4f : 25.4f / dpi;

    IIOMetadataNode child;/*  w  ww  .  j  ava 2 s .  c o  m*/

    child = getOrCreateChildNode(dimension, "HorizontalPixelSize");
    child.setAttribute("value", Double.toString(res));

    child = getOrCreateChildNode(dimension, "VerticalPixelSize");
    child.setAttribute("value", Double.toString(res));

    metadata.mergeTree(MetaUtil.STANDARD_METADATA_FORMAT, root);
}

From source file:com.ackpdfbox.app.imageio.TIFFUtil.java

/**
 * Updates the given ImageIO metadata with Sun's custom TIFF tags, as described in
 * the <a href="https://svn.apache.org/repos/asf/xmlgraphics/commons/tags/commons-1_3_1/src/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java">org.apache.xmlgraphics.image.writer.ImageIOTIFFImageWriter
 * sources</a>, /*from  w w w  . j  a  v  a2 s.  co m*/
 * the <a href="http://download.java.net/media/jai-imageio/javadoc/1.0_01/com/sun/media/imageio/plugins/tiff/package-summary.html">com.sun.media.imageio.plugins.tiff
 * package javadoc</a>
 * and the <a href="http://partners.adobe.com/public/developer/tiff/index.html">TIFF
 * specification</a>.
 *
 * @param image buffered image which will be written
 * @param metadata ImageIO metadata
 * @param dpi image dots per inch
 * @throws IIOInvalidTreeException if something goes wrong
 */
static void updateMetadata(IIOMetadata metadata, BufferedImage image, int dpi) throws IIOInvalidTreeException {
    debugLogMetadata(metadata, SUN_TIFF_FORMAT);

    if (!SUN_TIFF_FORMAT.equals(metadata.getNativeMetadataFormatName())) {
        LOG.debug("Using unknown TIFF image writer: " + metadata.getNativeMetadataFormatName());
        return;
    }

    IIOMetadataNode root = new IIOMetadataNode(SUN_TIFF_FORMAT);
    IIOMetadataNode ifd;
    if (root.getElementsByTagName("TIFFIFD").getLength() == 0) {
        ifd = new IIOMetadataNode("TIFFIFD");
        ifd.setAttribute("tagSets", tagSetClassName);
        root.appendChild(ifd);
    } else {
        ifd = (IIOMetadataNode) root.getElementsByTagName("TIFFIFD").item(0);
    }

    // standard metadata does not work, so we set the DPI manually
    ifd.appendChild(createRationalField(282, "XResolution", dpi, 1));
    ifd.appendChild(createRationalField(283, "YResolution", dpi, 1));
    ifd.appendChild(createShortField(296, "ResolutionUnit", 2)); // Inch

    ifd.appendChild(createLongField(278, "RowsPerStrip", image.getHeight()));
    ifd.appendChild(createAsciiField(305, "Software", "PDFBOX"));

    if (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() == 1) {
        // set PhotometricInterpretation WhiteIsZero
        // because of bug in Windows XP preview
        ifd.appendChild(createShortField(262, "PhotometricInterpretation", 0));
    }

    metadata.mergeTree(SUN_TIFF_FORMAT, root);

    debugLogMetadata(metadata, SUN_TIFF_FORMAT);
}

From source file:com.aegon.pdf2tiff.TIFFUtil.java

/**
 * Updates the given ImageIO metadata with Sun's custom TIFF tags.
 * {@see https://svn.apache.org/repos/asf/xmlgraphics/commons/tags/commons-1_3_1/src/java/org/
 *       apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java}
 * {@see http://download.java.net/media/jai-imageio/javadoc/1.0_01/com/sun/media/imageio/
 *       plugins/tiff/package-summary.html}
 * {@see http://partners.adobe.com/public/developer/tiff/index.html}
 * @param image buffered image which will be written
 * @param metadata ImageIO metadata/*from   ww w  . j  av a 2s. c om*/
 * @param dpi image dots per inch
 */
public static void updateMetadata(IIOMetadata metadata, BufferedImage image, int dpi) {
    debugLogMetadata(metadata, SUN_TIFF_FORMAT);

    if (!SUN_TIFF_FORMAT.equals(metadata.getNativeMetadataFormatName())) {
        LOG.debug("Using unknown TIFF image writer: " + metadata.getNativeMetadataFormatName());
        return;
    }

    IIOMetadataNode root = new IIOMetadataNode(SUN_TIFF_FORMAT);
    IIOMetadataNode ifd;
    if (root.getElementsByTagName("TIFFIFD").getLength() == 0) {
        ifd = new IIOMetadataNode("TIFFIFD");
        ifd.setAttribute("tagSets", "com.sun.media.imageio.plugins.tiff.BaselineTIFFTagSet");
        root.appendChild(ifd);
    } else {
        ifd = (IIOMetadataNode) root.getElementsByTagName("TIFFIFD").item(0);
    }

    // standard metadata does not work, so we set the DPI manually
    ifd.appendChild(createRationalField(282, "XResolution", dpi, 1));
    ifd.appendChild(createRationalField(283, "YResolution", dpi, 1));
    ifd.appendChild(createShortField(296, "ResolutionUnit", 2)); // Inch

    ifd.appendChild(createLongField(278, "RowsPerStrip", image.getHeight()));
    ifd.appendChild(createAsciiField(305, "Software", "PDFBOX"));

    if (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() == 1) {
        // set PhotometricInterpretation WhiteIsZero
        // because of bug in Windows XP preview
        ifd.appendChild(createShortField(262, "PhotometricInterpretation", 0));
    }

    try {
        metadata.mergeTree(SUN_TIFF_FORMAT, root);
    } catch (IIOInvalidTreeException e) {
        // should never happen
        throw new RuntimeException(e);
    }

    debugLogMetadata(metadata, SUN_TIFF_FORMAT);
}

From source file:nl.softwaredesign.exporter.ODTExportFormat.java

/**
 * TODO Move to utility class?//from www .  ja v  a  2  s.co  m
 * Extract the content data (bytes) from the given image.
 * @param icon image to get bytes for
 * @param format desired format for image
 * @param background Color to use as background if transparency in image
 * @param width Desired width of the image in the byte array
 * @param height Desired width of the image in the byte array
 * @return the bytes of the image
 * @throws IOException
 */
private byte[] imageToBytes(ImageIcon icon, String format, Color background, int width, int height)
        throws IOException {
    Iterator writers = ImageIO.getImageWritersByFormatName(format);
    ImageWriter writer = (ImageWriter) writers.next();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(output);
    writer.setOutput(ios);
    BufferedImage img;
    if ("png".equalsIgnoreCase(format) || "gif".equalsIgnoreCase(format)) {
        img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    } else {
        img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    }
    WaitingObserver observer = new WaitingObserver();
    if (!img.getGraphics().drawImage(icon.getImage(), 0, 0, width, height, background, observer)) {
        try {
            observer.waitForDone();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return new byte[0];
        }
    }
    IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(img),
            writer.getDefaultWriteParam());
    if (format.equalsIgnoreCase("jpg")) {
        Element tree = (Element) metadata.getAsTree("javax_imageio_jpeg_image_1.0");
        Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
        jfif.setAttribute("Xdensity", Integer.toString(72));
        jfif.setAttribute("Ydensity", Integer.toString(72));
        jfif.setAttribute("resUnits", "1");
        metadata.setFromTree("javax_imageio_jpeg_image_1.0", tree);
    } else {
        double dotsPerMilli = 7.2 / 2.54;
        IIOMetadataNode horiz = new IIOMetadataNode("HorizontalPixelSize");
        horiz.setAttribute("value", Double.toString(dotsPerMilli));
        IIOMetadataNode vert = new IIOMetadataNode("VerticalPixelSize");
        vert.setAttribute("value", Double.toString(dotsPerMilli));
        IIOMetadataNode dim = new IIOMetadataNode("Dimension");
        dim.appendChild(horiz);
        dim.appendChild(vert);
        IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0");
        root.appendChild(dim);
        metadata.mergeTree("javax_imageio_1.0", root);
    }

    writer.write(null, new IIOImage(img, null, metadata), null);
    return output.toByteArray();
}

From source file:org.apache.pdfbox.util.ImageIOUtil.java

private static void setDPI(IIOMetadata metadata, int dpi, String formatName) {
    IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(STANDARD_METADATA_FORMAT);

    IIOMetadataNode dimension = getOrCreateChildNode(root, "Dimension");

    // PNG writer doesn't conform to the spec which is
    // "The width of a pixel, in millimeters"
    // but instead counts the pixels per millimeter
    float res = "PNG".equals(formatName.toUpperCase()) ? dpi / 25.4f : 25.4f / dpi;

    IIOMetadataNode child;//  ww w .j a v a  2 s.  c o m

    child = getOrCreateChildNode(dimension, "HorizontalPixelSize");
    child.setAttribute("value", Double.toString(res));

    child = getOrCreateChildNode(dimension, "VerticalPixelSize");
    child.setAttribute("value", Double.toString(res));

    try {
        metadata.mergeTree(STANDARD_METADATA_FORMAT, root);
    } catch (IIOInvalidTreeException e) {
        // should never happen
        throw new RuntimeException(e);
    }
}

From source file:org.forgerock.doc.maven.PNGUtils.java

private static void setDPI(IIOMetadata metadata, final int dotsPerInch) throws IIOInvalidTreeException {

    final double inchesPerMillimeter = 1.0 / 25.4;
    final double dotsPerMillimeter = dotsPerInch * inchesPerMillimeter;

    IIOMetadataNode horizontalPixelSize = new IIOMetadataNode("HorizontalPixelSize");
    horizontalPixelSize.setAttribute("value", Double.toString(dotsPerMillimeter));

    IIOMetadataNode verticalPixelSize = new IIOMetadataNode("VerticalPixelSize");
    verticalPixelSize.setAttribute("value", Double.toString(dotsPerMillimeter));

    IIOMetadataNode dimension = new IIOMetadataNode("Dimension");
    dimension.appendChild(horizontalPixelSize);
    dimension.appendChild(verticalPixelSize);

    IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0");
    root.appendChild(dimension);//from   w w  w.  j av  a  2  s. co  m

    metadata.mergeTree("javax_imageio_1.0", root);
}