Example usage for javax.imageio.metadata IIOMetadataNode IIOMetadataNode

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

Introduction

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

Prototype

public IIOMetadataNode(String nodeName) 

Source Link

Document

Constructs an IIOMetadataNode with a given node name.

Usage

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

From source file:de.unigoettingen.sub.commons.contentlib.imagelib.JpegInterpreter.java

private void setNumLines(Node domNode, int lines) {
    Node markerSequenceNode = getFirstElementByName(domNode, "markerSequence");
    if (markerSequenceNode == null) {
        markerSequenceNode = new IIOMetadataNode("markerSequence");
        domNode.appendChild(markerSequenceNode);
    }/*from www  .j  a v a2 s  .c o  m*/

    Node sofNode = getFirstElementByName(markerSequenceNode, "sof");
    if (sofNode == null) {
        sofNode = new IIOMetadataNode("sof");
        markerSequenceNode.appendChild(sofNode);
    }

    Node attribute = getAttributeByName(sofNode, "numLines");
    if (attribute == null) {
        attribute = new IIOMetadataNode("numLines");
        sofNode.appendChild(attribute);
    }
    attribute.setNodeValue(Integer.toString(lines));
}

From source file:de.unigoettingen.sub.commons.contentlib.imagelib.JpegInterpreter.java

private void setSamplesPerLine(Node domNode, int samples) {
    Node markerSequenceNode = getFirstElementByName(domNode, "markerSequence");
    if (markerSequenceNode == null) {
        markerSequenceNode = new IIOMetadataNode("markerSequence");
        domNode.appendChild(markerSequenceNode);
    }/*from  w  w w .  j  a  va2s .co m*/

    Node sofNode = getFirstElementByName(markerSequenceNode, "sof");
    if (sofNode == null) {
        sofNode = new IIOMetadataNode("sof");
        markerSequenceNode.appendChild(sofNode);
    }

    Node attribute = getAttributeByName(sofNode, "samplesPerLine");
    if (attribute == null) {
        attribute = new IIOMetadataNode("samplesPerLine");
        sofNode.appendChild(attribute);
    }
    attribute.setNodeValue(Integer.toString(samples));
}

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

/**
 * TODO Move to utility class?/*from  w  w  w . j  av a2s .  c o 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.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 a  v  a2  s.c o m

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

From source file:org.photovault.imginfo.CreateCopyImageCommand.java

/**
 Helper function to save a rendered image to file
 @param instanceFile The file into which the image will be saved
 @param img Image that willb e saved//from w  ww .  j a v  a 2  s  . c o  m
 @param xmpData XPM metadata packet that should be saved with the image
 @throws PhotovaultException if saving does not succeed
 */
protected void saveImage(File instanceFile, RenderedImage img, byte[] xmpData) throws PhotovaultException {
    ImageOutputStream out = null;
    log.debug("Entry: saveImage, file = " + instanceFile.getAbsolutePath());
    try {
        out = new FileImageOutputStream(instanceFile);
    } catch (IOException e) {
        log.error("Error writing image: " + e.getMessage());
        throw new PhotovaultException(e.getMessage());
    }
    if (img.getSampleModel().getSampleSize(0) == 16) {
        log.debug("16 bit image, converting to 8 bits");
        double[] subtract = new double[1];
        subtract[0] = 0;
        double[] divide = new double[1];
        divide[0] = 1. / 256.;
        // Now we can rescale the pixels gray levels:
        ParameterBlock pbRescale = new ParameterBlock();
        pbRescale.add(divide);
        pbRescale.add(subtract);
        pbRescale.addSource(img);
        PlanarImage outputImage = (PlanarImage) JAI.create("rescale", pbRescale, null);
        // Make sure it is a byte image - force conversion.
        ParameterBlock pbConvert = new ParameterBlock();
        pbConvert.addSource(outputImage);
        pbConvert.add(DataBuffer.TYPE_BYTE);
        img = JAI.create("format", pbConvert);
    }

    IIOImage iioimg = new IIOImage(img, null, null);

    /*
     Not all encoders support metadata handling
     */
    Iterator writers = ImageIO.getImageWritersByFormatName("jpeg");
    ImageWriter imgwriter = null;
    while (writers.hasNext()) {
        imgwriter = (ImageWriter) writers.next();
        if (imgwriter.getClass().getName().endsWith("JPEGImageEncoder")) {
            // Break on finding the core provider.
            break;
        }
    }
    if (imgwriter == null) {
        System.err.println("Cannot find core JPEG writer!");
    }
    imgwriter.addIIOWriteWarningListener(new IIOWriteWarningListener() {

        public void warningOccurred(ImageWriter arg0, int arg1, String arg2) {
            log.warn("Warning from ImageWriter: " + arg2);
        }
    });
    ImageWriteParam params = imgwriter.getDefaultWriteParam();
    ImageTypeSpecifier its = ImageTypeSpecifier.createFromRenderedImage(img);
    IIOMetadata metadata = imgwriter.getDefaultImageMetadata(its, null);

    IIOMetadataNode metatop = (IIOMetadataNode) metadata.getAsTree("javax_imageio_jpeg_image_1.0");
    NodeList markerSeqNodes = metatop.getElementsByTagName("markerSequence");
    if (markerSeqNodes.getLength() > 0) {
        IIOMetadataNode xmpNode = new IIOMetadataNode("unknown");
        xmpNode.setAttribute("MarkerTag", "225");
        xmpNode.setUserObject(xmpData);
        markerSeqNodes.item(0).appendChild(xmpNode);
    }

    try {
        metadata.setFromTree("javax_imageio_jpeg_image_1.0", metatop);
    } catch (Exception e) {
        log.warn("error editing metadata: " + e.getMessage());
        e.printStackTrace();
        throw new PhotovaultException("error setting image metadata: \n" + e.getMessage());
    }

    iioimg.setMetadata(metadata);

    try {
        imgwriter.setOutput(out);
        imgwriter.write(iioimg);
    } catch (IOException e) {
        log.warn("Exception while encoding" + e.getMessage());
        throw new PhotovaultException(
                "Error writing instance " + instanceFile.getAbsolutePath() + ": " + e.getMessage());
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            log.warn("Exception while closing file: " + e.getMessage());
            imgwriter.dispose();
            throw new PhotovaultException(
                    "Error writing instance " + instanceFile.getAbsolutePath() + ": " + e.getMessage());

        }
        imgwriter.dispose();
    }
    log.debug("Exit: saveImage");
}