Example usage for javax.imageio.metadata IIOMetadataNode setAttribute

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

Introduction

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

Prototype

public void setAttribute(String name, String value) 

Source Link

Usage

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);// w ww . j  ava 2s. c om
    valueNode = new IIOMetadataNode("TIFFShort");
    arrayNode.appendChild(valueNode);
    valueNode.setAttribute("value", Integer.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);//  w ww  .  jav  a2s  .c  om
    valueNode = new IIOMetadataNode("TIFFAscii");
    arrayNode.appendChild(valueNode);
    valueNode.setAttribute("value", val);
    return field;
}

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);// www .  j a  v a2 s  .c  om
    valueNode = new IIOMetadataNode("TIFFLong");
    arrayNode.appendChild(valueNode);
    valueNode.setAttribute("value", Long.toString(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);//from ww w  .  j  ava2  s. c  o m
    valueNode = new IIOMetadataNode("TIFFRational");
    arrayNode.appendChild(valueNode);
    valueNode.setAttribute("value", numerator + "/" + denominator);
    return field;
}

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// ww w  .ja va 2  s .  co 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.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 av a 2 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.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;

    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.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() + "!");
    }/*  ww w  .j  a  v a2  s  .  com*/

    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);
    }//  w  w  w  . j  a va 2s.c o m

    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 image metadata in a tree using the following format <!ELEMENT
 * ch5.imageio.ch5image_1.00 (imageDimensions)> <!ATTLIST imageDimensions
 * imageWidth CDATA #REQUIRED imageHeight CDATA #REQUIRED
 *//*from  www.  j  a  v  a 2s  . c  om*/
private Node getNativeTree() {
    IIOMetadataNode root = new IIOMetadataNode(nativeMetadataFormatName);

    IIOMetadataNode node = new IIOMetadataNode("imageDimensions");
    node.setAttribute("imageWidth", Integer.toString(imageWidth));
    node.setAttribute("imageHeight", Integer.toString(imageHeight));
    root.appendChild(node);

    return root;
}