Note that there are some explanatory texts on larger screens.

plurals
  1. POJava ImageIO, unable to change metadata tree
    primarykey
    data
    text
    <p>I'm trying to use ImageIO to change the JPEG Comment field (COM) of a file. I read the metadata, do some changes to the DOM (change attribute of the comment field), then save / print the metadata. But my changes are not shown. This is the code (in a simple test class with static main()), 'file' is set.</p> <pre><code> ImageWriter writer = ImageIO.getImageWritersBySuffix("jpeg").next(); ImageReader reader = ImageIO.getImageReader(writer); reader.setInput(new FileImageInputStream(file)); RenderedImage img = reader.read(0); IIOMetadata meta = reader.getImageMetadata(0); IIOMetadataNode root1 = (IIOMetadataNode) meta.getAsTree("javax_imageio_jpeg_image_1.0"); String comment1 = getCommentFromDOM(root1); System.out.println("comment 1 = " + comment1); String comment2 = getCommentFromDOM(root1); System.out.println("comment 2 = " + comment2); meta.setFromTree("javax_imageio_jpeg_image_1.0", root1); //meta.reset(); IIOMetadataNode root2 = (IIOMetadataNode) meta.getAsTree("javax_imageio_jpeg_image_1.0"); String comment3 = getCommentFromDOM(root2); System.out.println("comment 3 = " + comment3); </code></pre> <p>The method getCommentFromDOM() just goes trough the tree and gets the comment attribute in the com tag, and then it changes the comment field but returns the original value.</p> <pre><code> ... if(md.getNodeName().equalsIgnoreCase("com")) { String comment = md.getAttribute("comment"); md.setAttribute("comment", "FOOOOOOOOOO"); return comment; } ... </code></pre> <p>So when this program is executed it will output:</p> <blockquote> <pre><code> comment 1 = originalcomment comment 2 = FOOOOOOOOOOO comment 3 = originalcomment </code></pre> </blockquote> <p>So the DOM was changed, but it looks like the meta.setFromTree() method did not use the new tree. I've also tried running meta.reset() which should clear the metadata, but that also seems to have no effect on the meta object.</p> <p>Can anyone see what I've done wrong in this very simple ImageIO test?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload