gpt4 book ai didi

java - Java 中 JPEG 压缩的自定义量化表

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:03:41 26 4
gpt4 key购买 nike

正如标题所说,我正在尝试使用自定义量化表来压缩 JPEG 格式的图像。我的问题是生成的文件无法打开,错误是:

Quantization table 0x00 was not defined

我的代码是这样的:

        JPEGImageWriteParam params = new JPEGImageWriteParam(null);
if (mQMatrix != null) {
JPEGHuffmanTable[] huffmanDcTables = {JPEGHuffmanTable.StdDCLuminance, JPEGHuffmanTable.StdDCChrominance};
JPEGHuffmanTable[] huffmanAcTables = {JPEGHuffmanTable.StdACLuminance, JPEGHuffmanTable.StdACChrominance};
dumpMatrices(mQMatrix);
params.setEncodeTables(mQMatrix, huffmanDcTables, huffmanAcTables);
}

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

Iterator writers = ImageIO.getImageWritersByFormatName("JPEG");
ImageWriter imageWriter = (ImageWriter) writers.next();

ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
imageWriter.setOutput(imageOutputStream);
imageWriter.write(null, new IIOImage(mSourceImage, null, null), params);

mCompressedImageSize = outputStream.size();

try (FileOutputStream fileOutputStream = new FileOutputStream(mOutFileName)) {
fileOutputStream.write(outputStream.toByteArray());

}
mCompressedImage = ImageIO.read(new ByteArrayInputStream(outputStream.toByteArray()));

我的猜测是它与元数据有关,但我没能找到解决方案。

谢谢,

更新:我使用十六进制查看器确定量化表(DQT - 0xFF、0xDB 部分)未写入输出文件。我假设我必须强制以某种方式编写它。

更新 2:因此在实际调试执行之后,我发现如果在参数对象中设置表,则不会为量化表和 Huffman 表生成元数据。如果缺少元数据,则不会将表写入文件。问题是我没有办法自定义元数据的内容。

最佳答案

非常有趣的问题,不幸的是,它并不平凡......这是我发现的:

首先,使用 JPEGImageWriteParam.setEncodeTables(...) 是行不通的。来自JavaDoc :

Sets the quantization and Huffman tables to use in encoding abbreviated streams.

JPEG Metadata Format Specification and Usage Notes进一步:

This ordering implements the design intention that tables should be included in JPEGImageWriteParams only as a means of specifying tables when no other source is available, and this can occur only when writing to an abbreviated stream without tables using known non-standard tables for compression.

即,param 选项只能用于编写“缩写流”(没有表格的自定义 JPEG,假设在回读时会提供表格)。结论:我们可以指定要使用 JPEG 编码的表格的唯一方法是将其传递到元数据中。

从上面提到的同一个文档中,元数据中的表将被忽略和替换,除非压缩模式是 MODE_COPY_FROM_METADATA,所以我们需要指定它。

参见 Image Metadata DTD有关元数据结构的文档。重要的部分是带有子节点的 dqtdht 节点,以及它们的“用户对象”(不要与普通 DOM“userData”混淆)。我们需要使用我们要使用的新表来更新这些节点。

这是我想出的代码:

// Obtain qtables
mQMatrix = ...;

// Read source image
ImageInputStream imageInputStream = ImageIO.createImageInputStream(...);
ImageReader reader = ImageIO.getImageReaders(imageInputStream).next();
reader.setInput(imageInputStream);

mSourceImage = reader.read(0);
IIOMetadata metadata = null;

// We need the imageWriter to create the default JPEG metadata
ImageWriter imageWriter = ImageIO.getImageWritersByFormatName("JPEG").next();

if (mQMatrix != null) {
dumpMatrices(mQMatrix);

// Obtain default image metadata data, in native JPEG format
metadata = imageWriter.getDefaultImageMetadata(ImageTypeSpecifier.createFromRenderedImage(m‌​SourceImage), null);
IIOMetadataNode nativeMeta = (IIOMetadataNode) metadata.getAsTree("javax_imageio_jpeg_image_1.0");

// Update dqt to values from mQMatrix
NodeList dqtables = nativeMeta.getElementsByTagName("dqtable");
for (int i = 0; i < dqtables.getLength(); i++) {
IIOMetadataNode dqt = (IIOMetadataNode) dqtables.item(i);
int dqtId = Integer.parseInt(dqt.getAttribute("qtableId"));
dqt.setUserObject(mQMatrix[dqtId]);
}

// For some reason, we need dht explicitly defined, when using MODE_COPY_FROM_METADATA...
NodeList dhtables = nativeMeta.getElementsByTagName("dhtable");

// Just use defaults for dht
JPEGHuffmanTable[] huffmanDcTables = {JPEGHuffmanTable.StdDCLuminance, JPEGHuffmanTable.StdDCChrominance};
JPEGHuffmanTable[] huffmanAcTables = {JPEGHuffmanTable.StdACLuminance, JPEGHuffmanTable.StdACChrominance};

// Update dht
for (int i = 0; i < dhtables.getLength(); i++) {
IIOMetadataNode dht = (IIOMetadataNode) dhtables.item(i);
int dhtClass = Integer.parseInt(dht.getAttribute("class")); // 0: DC, 1: AC
int dhtId = Integer.parseInt(dht.getAttribute("htableId"));

dht.setUserObject(dhtClass == 0 ? huffmanDcTables[dhtId] : huffmanAcTables[dhtId]);
}

// Merge updated tree back (important!)
metadata.mergeTree("javax_imageio_jpeg_image_1.0", nativeMeta);
}

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
imageWriter.setOutput(imageOutputStream);

// See http://docs.oracle.com/javase/6/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html#tables
JPEGImageWriteParam params = new JPEGImageWriteParam(null);
params.setCompressionMode(metadata == null ? MODE_DEFAULT : MODE_COPY_FROM_METADATA); // Unless MODE_COPY_FROM_METADATA, tables will be created!

imageWriter.write(null, new IIOImage(mSourceImage, null, metadata), params);
imageOutputStream.close();

mCompressedImageSize = outputStream.size();

try (FileOutputStream fileOutputStream = new FileOutputStream(mOutFileName)) {
fileOutputStream.write(outputStream.toByteArray());
}

mCompressedImage = ImageIO.read(new ByteArrayInputStream(outputStream.toByteArray()));

关于java - Java 中 JPEG 压缩的自定义量化表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25037227/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com