/** * Updates the metadata information based on the parameters to this writer. * * @param meta the metadata * @param params the parameters * @return the updated metadata */ protected IIOMetadata updateMetadata(IIOMetadata meta, ImageWriterParams params) { final String stdmeta = "javax_imageio_1.0"; if (meta.isStandardMetadataFormatSupported()) { IIOMetadataNode root = (IIOMetadataNode) meta.getAsTree(stdmeta); IIOMetadataNode dim = getChildNode(root, "Dimension"); IIOMetadataNode child; if (params.getResolution() != null) { child = getChildNode(dim, "HorizontalPixelSize"); if (child == null) { child = new IIOMetadataNode("HorizontalPixelSize"); dim.appendChild(child); } child.setAttribute("value", Double.toString(params.getResolution().doubleValue() / 25.4)); child = getChildNode(dim, "VerticalPixelSize"); if (child == null) { child = new IIOMetadataNode("VerticalPixelSize"); dim.appendChild(child); } child.setAttribute("value", Double.toString(params.getResolution().doubleValue() / 25.4)); } try { meta.mergeTree(stdmeta, root); } catch (IIOInvalidTreeException e) { throw new RuntimeException("Cannot update image metadata: " + e.getMessage()); } } return meta; }
/** * Merges <code>inData</code> into <code>outData</code>. The supplied metadata format name is * attempted first and failing that the standard metadata format name is attempted. */ private void convertMetadata(String metadataFormatName, IIOMetadata inData, IIOMetadata outData) { String formatName = null; String nativeFormatName = inData.getNativeMetadataFormatName(); if (nativeFormatName != null && nativeFormatName.equals(metadataFormatName)) { formatName = metadataFormatName; } else { String[] extraFormatNames = inData.getExtraMetadataFormatNames(); if (extraFormatNames != null) { for (int i = 0; i < extraFormatNames.length; i++) { if (extraFormatNames[i].equals(metadataFormatName)) { formatName = metadataFormatName; break; } } } } if (formatName == null && inData.isStandardMetadataFormatSupported()) { formatName = STANDARD_METADATA_NAME; } if (formatName != null) { try { Node root = inData.getAsTree(formatName); outData.mergeTree(formatName, root); } catch (IIOInvalidTreeException e) { // ignore } } }