public static void main(String[] args) throws IOException { String fileName = "nomarkers.jpg"; String sep = System.getProperty("file.separator"); String dir = System.getProperty("test.src", "."); String filePath = dir+sep+fileName; System.out.println("Test file: " + filePath); File file = new File(filePath); ImageInputStream stream = ImageIO.createImageInputStream(file); Iterator<ImageReader> readers = ImageIO.getImageReaders(stream); if(readers.hasNext()) { ImageReader reader = readers.next(); reader.setInput(stream); IIOMetadata metadata = reader.getImageMetadata(0); IIOMetadataNode standardTree = (IIOMetadataNode) metadata.getAsTree (IIOMetadataFormatImpl.standardMetadataFormatName); IIOMetadataNode colorSpaceType = (IIOMetadataNode) standardTree.getElementsByTagName("ColorSpaceType").item(0); String colorSpaceName = colorSpaceType.getAttribute("name"); if(colorSpaceName.equals("RGB")) throw new RuntimeException("Identified incorrect ColorSpace"); } }
public Node getAsTree(String formatName) { if (formatName == null) { throw new IllegalArgumentException("null formatName!"); } if (isStream) { if (formatName.equals(JPEG.nativeStreamMetadataFormatName)) { return getNativeTree(); } } else { if (formatName.equals(JPEG.nativeImageMetadataFormatName)) { return getNativeTree(); } if (formatName.equals (IIOMetadataFormatImpl.standardMetadataFormatName)) { return getStandardTree(); } } throw new IllegalArgumentException("Unsupported format name: " + formatName); }
public void setFromTree(String formatName, Node root) throws IIOInvalidTreeException { if (formatName == null) { throw new IllegalArgumentException("null formatName!"); } if (root == null) { throw new IllegalArgumentException("null root!"); } if (isStream && (formatName.equals(JPEG.nativeStreamMetadataFormatName))) { setFromNativeTree(root); } else if (!isStream && (formatName.equals(JPEG.nativeImageMetadataFormatName))) { setFromNativeTree(root); } else if (!isStream && (formatName.equals (IIOMetadataFormatImpl.standardMetadataFormatName))) { // In this case a reset followed by a merge is correct super.setFromTree(formatName, root); } else { throw new IllegalArgumentException("Unsupported format name: " + formatName); } }
public void mergeTree(String formatName, Node root) throws IIOInvalidTreeException { if (formatName.equals(nativeMetadataFormatName)) { if (root == null) { throw new IllegalArgumentException("root == null!"); } mergeNativeTree(root); } else if (formatName.equals (IIOMetadataFormatImpl.standardMetadataFormatName)) { if (root == null) { throw new IllegalArgumentException("root == null!"); } mergeStandardTree(root); } else { throw new IllegalArgumentException("Not a recognized format!"); } }
/** * Converts a standard {@code javax_imageio_1.0} tree to a * {@code TIFFImageMetadata} object. * * @param inData The metadata object. * @return a {@code TIFFImageMetadata} or {@code null} if * the standard tree derived from the input object is {@code null}. * @throws IllegalArgumentException if {@code inData} is * {@code null}. * @throws IllegalArgumentException if {@code inData} does not support * the standard metadata format. * @throws IIOInvalidTreeException if {@code inData} generates an * invalid standard metadata tree. */ private TIFFImageMetadata convertStandardImageMetadata(IIOMetadata inData) throws IIOInvalidTreeException { if(inData == null) { throw new NullPointerException("inData == null!"); } else if(!inData.isStandardMetadataFormatSupported()) { throw new IllegalArgumentException ("inData does not support standard metadata format!"); } TIFFImageMetadata outData = null; String formatName = IIOMetadataFormatImpl.standardMetadataFormatName; Node tree = inData.getAsTree(formatName); if (tree != null) { List<TIFFTagSet> tagSets = new ArrayList<TIFFTagSet>(1); tagSets.add(BaselineTIFFTagSet.getInstance()); outData = new TIFFImageMetadata(tagSets); outData.setFromTree(formatName, tree); } return outData; }
public void mergeTree(String formatName, Node root) throws IIOInvalidTreeException{ if (formatName.equals(nativeMetadataFormatName)) { if (root == null) { throw new NullPointerException("root == null!"); } mergeNativeTree(root); } else if (formatName.equals (IIOMetadataFormatImpl.standardMetadataFormatName)) { if (root == null) { throw new NullPointerException("root == null!"); } mergeStandardTree(root); } else { throw new IllegalArgumentException("Not a recognized format!"); } }
public static void main(String[] args) throws IOException { // Generate some trivial image and save it to a temporary array ByteArrayOutputStream tmp = new ByteArrayOutputStream(); ImageIO.write(new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB), "gif", tmp); // Read the stream ImageInputStream in = new MemoryCacheImageInputStream( new ByteArrayInputStream(tmp.toByteArray())); ImageReader reader = ImageIO.getImageReaders(in).next(); reader.setInput(in); // Retrieve standard image metadata tree IIOMetadata meta = reader.getImageMetadata(0); if (meta == null || !meta.isStandardMetadataFormatSupported()) { throw new Error("Test failure: Missing metadata"); } Element root = (Element) meta. getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName); // Test getElementsByTagName("*") if (root.getElementsByTagName("*").getLength() == 0) { throw new RuntimeException("getElementsByTagName(\"*\") returns" + " nothing"); } }