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: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  w  w  w.j av  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;
}

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

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

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

From source file:org.pentaho.big.data.kettle.plugins.hbase.input.HBaseInputMetaTest.java

@Test
public void testLoadXmlDoesntBubleUpException() throws Exception {
    KettleLogStore.init();/*  w w w  .j av a2s  . c  o m*/
    ClusterInitializationException exception = new ClusterInitializationException(new Exception());
    hBaseInputMeta.setNamedCluster(namedCluster);
    when(namedClusterServiceLocator.getService(namedCluster, HBaseService.class)).thenThrow(exception);
    when(namedClusterService.getClusterTemplate()).thenReturn(namedCluster);

    IIOMetadataNode node = new IIOMetadataNode();
    IIOMetadataNode child = new IIOMetadataNode("disable_wal");
    IIOMetadataNode grandChild = new IIOMetadataNode();
    grandChild.setNodeValue("N");
    child.appendChild(grandChild);
    node.appendChild(child);

    hBaseInputMeta.loadXML(node, new ArrayList<>(), metaStore);

    ServiceStatus serviceStatus = hBaseInputMeta.getServiceStatus();
    assertNotNull(serviceStatus);
    assertFalse(serviceStatus.isOk());
    assertEquals(exception, serviceStatus.getException());
}

From source file:org.pentaho.big.data.kettle.plugins.hbase.input.HBaseInputMetaTest.java

@Test
public void testLoadXmlServiceStatusOk() throws Exception {
    KettleLogStore.init();//from  w w  w.  j  ava2  s .  c om
    hBaseInputMeta.setNamedCluster(namedCluster);
    when(namedClusterServiceLocator.getService(namedCluster, HBaseService.class)).thenReturn(hBaseService);
    when(namedClusterService.getClusterTemplate()).thenReturn(namedCluster);
    when(hBaseService.getHBaseValueMetaInterfaceFactory())
            .thenReturn(mock(HBaseValueMetaInterfaceFactory.class));
    MappingFactory mappingFactory = mock(MappingFactory.class);
    when(hBaseService.getMappingFactory()).thenReturn(mappingFactory);
    when(mappingFactory.createMapping()).thenReturn(mock(Mapping.class));

    IIOMetadataNode node = new IIOMetadataNode();
    IIOMetadataNode child = new IIOMetadataNode("disable_wal");
    IIOMetadataNode grandChild = new IIOMetadataNode();
    grandChild.setNodeValue("N");
    child.appendChild(grandChild);
    node.appendChild(child);

    hBaseInputMeta.loadXML(node, new ArrayList<>(), metaStore);

    ServiceStatus serviceStatus = hBaseInputMeta.getServiceStatus();
    assertNotNull(serviceStatus);
    assertTrue(serviceStatus.isOk());
}