以下代码使用十二猴子创建一个 tiff 文件。创建的文件会自动刷新到磁盘。但我希望该文件仅在内存中创建。如何使用 ImageOutputStream 实现这一目标?
try {
// Create output stream
ImageOutputStream output = ImageIO.createImageOutputStream(targetFile);
try {
writer.setOutput(output);
ImageWriteParam param = writer.getDefaultWriteParam();
TIFFImageWriteParam tiffWriteParam = (TIFFImageWriteParam) param;
tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);
TIFFImageMetadata metadata = (TIFFImageMetadata) createMetadata(sourceFile);
// Set dpi
String nativeFormat = TIFFMedataFormat.SUN_NATIVE_IMAGE_METADATA_FORMAT_NAME;
// Node nativeTree = metadata.getAsTree(nativeFormat);
IIOMetadataNode newTree =
new IIOMetadataNode("com_sun_media_imageio_plugins_tiff_image_1.0");
IIOMetadataNode ifdNode = new IIOMetadataNode("TIFFIFD");
newTree.appendChild(ifdNode);
createTIFFFieldNode(ifdNode, TIFF.TAG_RESOLUTION_UNIT, TIFF.TYPE_SHORT, 2);
createTIFFFieldNode(ifdNode, TIFF.TAG_X_RESOLUTION, TIFF.TYPE_RATIONAL, new Rational(300));
createTIFFFieldNode(ifdNode, TIFF.TAG_Y_RESOLUTION, TIFF.TYPE_RATIONAL, new Rational(300));
metadata.mergeTree(nativeFormat, newTree);
IIOImage iioimage = new IIOImage(image, null, metadata);
writer.write(metadata, iioimage, tiffWriteParam);
} finally {
output.close();
}
} finally {
writer.dispose();
}