JPEG metadata always read-only
See original GitHub issueGreetings. I’m trying to read and modify metadata of JPEG images. Let’s look at the following (heavily simplified) code:
try (final Closeable imageInputStream = ImageIO.createImageInputStream(new File("D:\\testImage.jpg"))) {
final ImageReader reader = ImageIO.getImageReaders(imageInputStream).next();
reader.setInput(imageInputStream);
final IIOImage imageAndMeta = reader.readAll(0, reader.getDefaultReadParam());
reader.dispose();
final IIOMetadata metadata = imageAndMeta.getMetadata();
final Node root = new IIOMetadataNode("javax_imageio_jpeg_image_1.0");
root.appendChild(new IIOMetadataNode("JPEGvariety"));
root.appendChild(new IIOMetadataNode("markerSequence"));
//root.appendChild(...somethingElse...)
metadata.mergeTree("javax_imageio_jpeg_image_1.0", root);
} catch (final Exception error) {
error.printStackTrace();
}
It works fine without TwelveMonkeys, but throws an error (only) when TwelveMonkeys is installed:
java.lang.IllegalStateException: Metadata is read-only
at com.twelvemonkeys.imageio.AbstractMetadata.assertMutable(AbstractMetadata.java:117) ~[imageio-core-3.8.1.jar:3.8.1]
at com.twelvemonkeys.imageio.AbstractMetadata.mergeTree(AbstractMetadata.java:96) ~[imageio-core-3.8.1.jar:3.8.1]
...
It seems that the metadata is always read-only and thus mergeTree function does not work. I’ve looked into this with debugger and it seems that in TwelveMonkeys’ JPEGImage10Metadata.java
isReadOnly()
always returns true while in the original com.sun.imageio.plugins.jpeg.JPEGMetadata
it’s always false. Any idea why is this and how could I get around this?
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
How to Read and Remove Metadata from Your Photos ... - Auth0
The code opens each file as read-only. It reads the file's data in binary format into a file object, which it then uses...
Read more >How do I make Metadata read-only? - Microsoft Community
After reading about how the creation and modification dates on files can be changed by something as insignificant as a copy operation (a...
Read more >What Is EXIF Data? 3 Ways to Remove Metadata From Photos
Most cameras embed hidden information ("metadata") in photographs taken. For privacy's sake, here's how to remove that metadata.
Read more >Making a jpeg Read only for others? - Seven Forums
No way at all. Data can only be controlled as long as trusted people are the only with write access to it. And...
Read more >JPEG Metadata Format Specification and Usage Notes
JPEG metadata consists of the data contained in marker segments in a JPEG stream. The image metadata object ... Such an image may...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi Harald. I’ve updated to v3.8.3 and the “Invalid DHT node” error is indeed gone now. Thank you.
No, it gives you a mutable JPEG metadata, so you can copy whatever you like (what this issue is about).
Copying only Exif is (probably) possible, but tedious…
To copy only the Exif segment, you need to look at the
"markerSequence"
, get all the"unknown"
nodes, and get the ones with"MarkerTag"
value"225"
(APP1/0xFFE1
). There might be other APP1 markers beside Exif, so you probably need to look into theuserObject
, which for all"unknown"
segments will contain abyte
array. For Exif, this array will start with the stringExif\0\0
(and be followed by the TIFF structure).Sadly, there is a bug in the JDK metadata, that inserts multiple
"unknown"
markers when you merge… 😕 Workaround for this is to merge the nodes first, then usesetFromTree
. Again, unnecessary hard work for something that should be simple.I’d love to fix it, but proposing PRs to OpenJDK often takes several times that of a workaround… If only there was more time.