我正在尝试将BufferedImage保存为TIFF。这样可以正常工作,但每TIFF花费的时间太长,最长可达5秒。 5秒可能听起来不是很多,但如果你想保存多达20张图像太长了。
这是我的代码:
public void convertToTif() throws ConvertionFailedException {
final int dpi = 300;
try {
ImageOutputStream ios = null;
TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi();
ImageWriter writer = tiffspi.createWriterInstance();
// setup writer
ios = ImageIO.createImageOutputStream(this.tifFile);
writer.setOutput(ios);
BufferedImage tiffImage = create1BitTif(this.imageToConvert);
// get new Metadata
ImageTypeSpecifier imageType = ImageTypeSpecifier.createFromBufferedImageType(tiffImage.getType());
IIOMetadata imageMetadata = writer.getDefaultImageMetadata(imageType, null);
TIFFDirectory dir = TIFFDirectory.createFromMetadata(imageMetadata);
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
TIFFTag tagXRes = base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION);
TIFFTag tagYRes = base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
TIFFTag tagNewSubfile = base.getTag(BaselineTIFFTagSet.TAG_NEW_SUBFILE_TYPE);
TIFFTag tagBitsPerSample = base.getTag(BaselineTIFFTagSet.TAG_BITS_PER_SAMPLE);
TIFFTag tagResolutionUnit = base.getTag(BaselineTIFFTagSet.TAG_RESOLUTION_UNIT);
// set X DPI
long[][] X_DPI = new long[][] { { dpi, 1 } };
TIFFField xDPI = new TIFFField(tagXRes, TIFFTag.TIFF_RATIONAL, 1, X_DPI);
// set Y DPI
long[][] Y_DPI = new long[][] { { dpi, 1 } };
TIFFField yDPI = new TIFFField(tagYRes, TIFFTag.TIFF_RATIONAL, 1, Y_DPI);
// setzt Photometric von BlackIsZero auf WhiteIsZero
TIFFField photometricInterpretationField = new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_PHOTOMETRIC_INTERPRETATION), BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO);
// setzt NewSubfileType auf 0
TIFFField newSubfile = new TIFFField(tagNewSubfile, 0);
// setzt BitsPerSample auf 1
TIFFField bitsPerSample = new TIFFField(tagBitsPerSample, 1);
// setzt ResolutionUnit auf 2
TIFFField resolutionUnit = new TIFFField(tagResolutionUnit, 2);
dir.addTIFFField(xDPI);
dir.addTIFFField(yDPI);
dir.addTIFFField(photometricInterpretationField);
dir.addTIFFField(newSubfile);
dir.addTIFFField(bitsPerSample);
dir.addTIFFField(resolutionUnit);
TIFFImageWriteParam param = (TIFFImageWriteParam) writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setTIFFCompressor(new TIFFT6Compressor());
param.setCompressionType("CCITT T.6");
param.setCompressionQuality(0.0f);
IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(null);
String tiffMetadataFormatName = streamMetadata.getNativeMetadataFormatName();
IIOMetadataNode newTree = new IIOMetadataNode(tiffMetadataFormatName);
// create little endian
IIOMetadataNode endianNode = new IIOMetadataNode("ByteOrder");
endianNode.setAttribute("value", "LITTLE_ENDIAN");
newTree.appendChild(endianNode);
streamMetadata.setFromTree(tiffMetadataFormatName, newTree);
writer.prepareWriteSequence(streamMetadata);
writer.writeToSequence(new IIOImage(tiffImage, null, dir.getAsMetadata()), param);
ios.close();
} catch (IOException e) {
throw new ConvertionFailedException(e);
}
}