我的應用程序負責使用特定的alghoritm將單個TIFF文件分割成多個較小的文件。一切正常,但我關心的是,應用程序生成的文件大小超過原始文件。圖像壓縮和ImageIO庫
應用程序處理的原始文件總大小約爲26mb,生成的文件總大小等於387mb!下面是一個過程的代碼片段 - 我是一個圖像壓縮和ImageIO庫的amator,並且一直沒能找到任何有用的東西,因此我想問問是否有什麼我可以改變的那些結果更接近。理想情況下,我想使用與原始相同的壓縮。
final ImageWriter writer = ImageIO.getImageWritersByFormatName(resultsExtension).next();
final ImageWriteParam writeParams = writer.getDefaultWriteParam();
writeParams.setCompressionMode(ImageWriteParam.MODE_COPY_FROM_METADATA);
BufferedImage page = ImageUtils.getSinglePageFromTiffFile(documentToSplit, currentPageIndex);
while (currentPageIndex < pagesQty) {
OutputStream outStream = null;
ImageOutputStream imgOutStream = null;
try {
outStream = new FileOutputStream(newDocFile);
imgOutStream = ImageIO.createImageOutputStream(outStream);
writer.setOutput(imgOutStream);
writer.prepareWriteSequence(null);
writer.writeToSequence(new IIOImage(page, null, null), writeParams);
currentPageIndex++;
if (CONDITION) {
writer.endWriteSequence();
break;
}
writer.writeToSequence(new IIOImage(page, null, null), writeParams);
currentPageIndex++;
}
} finally {
if (imgOutStream != null) {
imgOutStream.close();
}
if (outStream != null) {
outStream.close();
}
}
}
getSinglePageFromTiffFile
方法:
public static BufferedImage getSinglePageFromTiffFile(File file, int pageIndex)
throws IOException {
ImageInputStream is = ImageIO.createImageInputStream(file);
ImageReader reader;
try {
reader = ImageIO.getImageReaders(is).next();
reader.setInput(is);
return reader.read(pageIndex);
} finally {
if (is != null) {
is.close();
}
}
}
如果您想將多頁TIFF拆分爲多個單頁TIFF,您可能需要查看[this Q/A](https://stackoverflow.com/q/44565928/1428606),其中演示瞭如何執行這不需要解碼圖像,從而保持原始的壓縮/文件大小。 – haraldK