Example usage for javax.imageio.metadata IIOMetadataNode appendChild

List of usage examples for javax.imageio.metadata IIOMetadataNode appendChild

Introduction

In this page you can find the example usage for javax.imageio.metadata IIOMetadataNode appendChild.

Prototype

public Node appendChild(Node newChild) 

Source Link

Document

Adds the node newChild to the end of the list of children of this node.

Usage

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

private static IIOMetadataNode createLongField(int number, String name, long val) {
    IIOMetadataNode field, arrayNode, valueNode;
    field = new IIOMetadataNode("TIFFField");
    field.setAttribute("number", Integer.toString(number));
    field.setAttribute("name", name);
    arrayNode = new IIOMetadataNode("TIFFLongs");
    field.appendChild(arrayNode);
    valueNode = new IIOMetadataNode("TIFFLong");
    arrayNode.appendChild(valueNode);//from   w  ww.j a  v  a  2 s .  c  om
    valueNode.setAttribute("value", Long.toString(val));
    return field;
}

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

private static IIOMetadataNode createAsciiField(int number, String name, String val) {
    IIOMetadataNode field, arrayNode, valueNode;
    field = new IIOMetadataNode("TIFFField");
    field.setAttribute("number", Integer.toString(number));
    field.setAttribute("name", name);
    arrayNode = new IIOMetadataNode("TIFFAsciis");
    field.appendChild(arrayNode);
    valueNode = new IIOMetadataNode("TIFFAscii");
    arrayNode.appendChild(valueNode);/*from   www  . j  a va2s  .c  o m*/
    valueNode.setAttribute("value", val);
    return field;
}

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

private static IIOMetadataNode createRationalField(int number, String name, int numerator, int denominator) {
    IIOMetadataNode field, arrayNode, valueNode;
    field = new IIOMetadataNode("TIFFField");
    field.setAttribute("number", Integer.toString(number));
    field.setAttribute("name", name);
    arrayNode = new IIOMetadataNode("TIFFRationals");
    field.appendChild(arrayNode);
    valueNode = new IIOMetadataNode("TIFFRational");
    arrayNode.appendChild(valueNode);//from  w ww.j  a  va2  s  .c o m
    valueNode.setAttribute("value", numerator + "/" + denominator);
    return field;
}

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

private static IIOMetadataNode createShortField(int tiffTagNumber, String name, int val) {
    IIOMetadataNode field, arrayNode, valueNode;
    field = new IIOMetadataNode("TIFFField");
    field.setAttribute("number", Integer.toString(tiffTagNumber));
    field.setAttribute("name", name);
    arrayNode = new IIOMetadataNode("TIFFShorts");
    field.appendChild(arrayNode);
    valueNode = new IIOMetadataNode("TIFFShort");
    arrayNode.appendChild(valueNode);//  w  ww  .  j  a  v a 2 s.c om
    valueNode.setAttribute("value", Integer.toString(val));
    return field;
}

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  ww w.j  a va  2  s .  c o 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/*  w w w .j av a2s  . c o  m*/
 * @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:com.ackpdfbox.app.imageio.ImageIOUtil.java

/**
 * Gets the named child node, or creates and attaches it.
 *
 * @param parentNode the parent node//w  w  w.java 2 s.c o m
 * @param name name of the child node
 *
 * @return the existing or just created child node
 */
private static IIOMetadataNode getOrCreateChildNode(IIOMetadataNode parentNode, String name) {
    NodeList nodeList = parentNode.getElementsByTagName(name);
    if (nodeList != null && nodeList.getLength() > 0) {
        return (IIOMetadataNode) nodeList.item(0);
    }
    IIOMetadataNode childNode = new IIOMetadataNode(name);
    parentNode.appendChild(childNode);
    return childNode;
}

From source file:com.sketchy.utils.image.SketchyImage.java

public static void save(SketchyImage sketchyImage, File file) throws Exception {
    if (!file.getParentFile().canWrite()) {
        throw new Exception("Can not write to File: " + file.getPath() + "!");
    }//from  w w w  .j av  a  2s  . c  om

    if (!StringUtils.endsWithIgnoreCase(file.getName(), ".png")) {
        throw new Exception("Can not save SketchyImage! Must be a .png file!");
    }

    Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByFormatName("png");
    ImageWriter imageWriter = null;
    if (imageWriters.hasNext()) { // Just get first one
        imageWriter = imageWriters.next();
    }
    if (imageWriter == null) {
        // this should never happen!! if so.. we got problems
        throw new Exception("Can not find ImageReader for .png Files!");
    }

    ImageOutputStream os = null;
    try {
        os = ImageIO.createImageOutputStream(file);
        imageWriter.setOutput(os);

        ImageWriteParam imageWriterParam = imageWriter.getDefaultWriteParam();
        IIOMetadata metadata = imageWriter.getDefaultImageMetadata(
                ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_BINARY),
                imageWriterParam);

        String metaDataFormatName = metadata.getNativeMetadataFormatName();
        IIOMetadataNode metaDataNode = (IIOMetadataNode) metadata.getAsTree(metaDataFormatName);

        NodeList childNodes = metaDataNode.getElementsByTagName("pHYs");
        IIOMetadataNode physNode = null;
        if (childNodes.getLength() == 0) {
            physNode = new IIOMetadataNode("pHYs");
            physNode.setAttribute("pixelsPerUnitXAxis",
                    Integer.toString((int) Math.ceil(sketchyImage.dotsPerMillimeterWidth * 1000)));
            physNode.setAttribute("pixelsPerUnitYAxis",
                    Integer.toString((int) Math.ceil(sketchyImage.dotsPerMillimeterHeight * 1000)));
            physNode.setAttribute("unitSpecifier", "meter"); // always meter
            metaDataNode.appendChild(physNode);
        } else {
            for (int nodeIdx = 0; nodeIdx < childNodes.getLength(); nodeIdx++) {
                physNode = (IIOMetadataNode) childNodes.item(nodeIdx);
                physNode.setAttribute("pixelsPerUnitXAxis",
                        Integer.toString((int) Math.ceil(sketchyImage.dotsPerMillimeterWidth * 1000)));
                physNode.setAttribute("pixelsPerUnitYAxis",
                        Integer.toString((int) Math.ceil(sketchyImage.dotsPerMillimeterHeight * 1000)));
                physNode.setAttribute("unitSpecifier", "meter"); // always meter
                metaDataNode.appendChild(physNode);
            }
        }
        metadata.setFromTree(metaDataFormatName, metaDataNode);
        imageWriter.write(new IIOImage(sketchyImage.image, null, metadata));
        os.flush();
    } catch (Exception e) {
        throw new Exception("Error Saving SketchyImage File: " + file.getPath() + "!  " + e.getMessage());
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:net.d53dev.dslfy.web.service.GifService.java

private void configureGIFFrame(IIOMetadata meta, String delayTime, int imageIndex, String disposalMethod,
        int loopCount) {
    String metaFormat = meta.getNativeMetadataFormatName();

    if (!"javax_imageio_gif_image_1.0".equals(metaFormat)) {
        throw new IllegalArgumentException("Unfamiliar gif metadata format: " + metaFormat);
    }//from w  w w .  j  ava 2 s .  com

    Node root = meta.getAsTree(metaFormat);

    Node child = root.getFirstChild();
    while (child != null) {
        if ("GraphicControlExtension".equals(child.getNodeName()))
            break;
        child = child.getNextSibling();
    }

    IIOMetadataNode gce = (IIOMetadataNode) child;
    gce.setAttribute("userDelay", "FALSE");
    gce.setAttribute("delayTime", delayTime);
    gce.setAttribute("disposalMethod", disposalMethod);

    if (imageIndex == 0) {
        IIOMetadataNode aes = new IIOMetadataNode("ApplicationExtensions");
        IIOMetadataNode ae = new IIOMetadataNode("ApplicationExtension");
        ae.setAttribute("applicationID", "NETSCAPE");
        ae.setAttribute("authenticationCode", "2.0");
        byte[] uo = new byte[] { 0x1, (byte) (loopCount & 0xFF), (byte) ((loopCount >> 8) & 0xFF) };
        ae.setUserObject(uo);
        aes.appendChild(ae);
        root.appendChild(aes);
    }

    try {
        meta.setFromTree(metaFormat, root);
    } catch (IIOInvalidTreeException e) {
        throw new Error(e);
    }
}

From source file:ch5ImageReader.java

/**
 * returns the stream metadata in a tree using the following format
 * <!ELEMENT ch5.imageio.ch5stream_1.00 (imageDimensions)> <!ATTLIST
 * imageDimensions numberImages CDATA #REQUIRED
 */// ww w  .  j  a va 2 s  .  com
private Node getNativeTree() {
    IIOMetadataNode node; // scratch node

    IIOMetadataNode root = new IIOMetadataNode(nativeMetadataFormatName);

    // Image descriptor
    node = new IIOMetadataNode("imageDimensions");
    node.setAttribute("numberImages", Integer.toString(numberImages));
    root.appendChild(node);

    return root;
}