在Java中读取jpeg文件的量化表

在Java中读取jpeg文件的量化表

本文介绍了在Java中读取jpeg文件的量化表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索一个Java库,它有一个用于返回图像DQT的接口。我知道,例如exiftool可以做到,但Java包装器似乎没有接口。任何指针都表示赞赏。一些JNI绑定也可能适用于我。

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.

提前致谢!

推荐答案

您可以使用ImageIO的 JPEGImageReader IIOMetadata 获取DQT。

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.

我总是发现 IIOMetadata akward并且不方便,但是它完成了工作(除了一些非spec JPEG文件)。

I've always found the IIOMetadataakward and inconvenient, but it does the job (except for some non-spec JPEG files).

如果您不喜欢API,那么自己解析JFIF流和DQT段并不困难。灵感来自和一些C示例代码,我写了,它解析DQT并尝试重新估计JPEG的质量设置。

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.

这篇关于在Java中读取jpeg文件的量化表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:47