I'm searching for a Java library that has an interface for returning DQT of an image. I know that e.g. exiftool can do it, but the Java wrapper does not seem to have an interface for that. Any pointers are appreciated. Some JNI bindings will probably work for me as well.
Thanks in advance!
解决方案
You can get the DQTs from the IIOMetadata using ImageIO's JPEGImageReader.
IIOMetadata metadata = jpegReader.getImageMetadata();
IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(metadata.getNativeMetadataFormatName());
NodeList dqt = root.getElementsByTagName("dqt");
NodeList dqtables = ((IIOMetadataNode) dqt.item(0)).getElementsByTagName("dqtable");
JPEGQTable[] qTables = new JPEGQTable[dqtables.getLength()];
for (int i = 0; i < dqtables.getLength(); i++) {
qTables[i] = (JPEGQTable) ((IIOMetadataNode) dqtables.item(i)).getUserObject();
System.out.println("qTables: " + qTables[i]);
}
See Image Metadata DTD for docs on what data is available and how to access it.
I've always found the IIOMetadataakward and inconvenient, but it does the job (except for some non-spec JPEG files).
If you don't like the API, it's not too hard to parse the JFIF stream and the DQT segments yourself. Inspired by an old blog post and some C sample code, I wrote JPEGQuality, that parses the DQT and also tries to re-estimate the quality setting of the JPEG.