当我尝试将“.jpg”文件的内容复制到新的“.tif”文件时,不会保留 IIOMetadata。我需要保留的主要元数据标签是:compressionTypeName、horizontalPixelSize 和 VerticalPixelSize。
这是复制过程的简化代码:
public void stackOverflowQuestion(File fileToRead) {
ImageReader reader;
ImageWriter writer=null;
FileImageInputStream fileImageInputStream;
BufferedImage bufferedImageReadImage;
IIOMetadata iioMetadataReadImage;
IIOMetadata iioMetaDataImageToWrite;
IIOImage iioImage;
ImageWriteParam imageWriteParams;
ImageReadParam imageReadParam;
File newFile;
ImageTypeSpecifier imageTypeSpecifierDestino;
reader = this.createImageReaderBySuffix("jpg");//(parameter’s extension)
try {
fileImageInputStream=new FileImageInputStream(fileToRead);
reader.setInput(fileImageInputStream);
writer = ImageIO.getImageWritersBySuffix("tif").next();
imageReadParam=reader.getDefaultReadParam();
bufferedImageReadImage = reader.read(0,imageReadParam);
iioMetadataReadImage = reader.getImageMetadata(0);
if (bufferedImageReadImage!=null) {
newFile =getNewFile();//simply gets a new empty file.
writer.setOutput(new FileImageOutputStream(newFile));
writer.prepareWriteSequence(null);
imageTypeSpecifierDestino = ImageTypeSpecifier.createFromBufferedImageType(bufferedImageReadImage.getType());
imageWriteParams = (ImageWriteParam) writer.getDefaultWriteParam();
imageWriteParams.setDestinationType( ImageTypeSpecifier.createFromBufferedImageType( bufferedImageReadImage.getType()) ) ;
imageWriteParams.setCompressionMode( ImageWriteParam.MODE_COPY_FROM_METADATA );
iioMetaDataImageToWrite= writer.convertImageMetadata(iioMetadataReadImage, imageTypeSpecifierDestino, imageWriteParams);
//at this point, the metadata is spoiled
iioImage=new IIOImage(bufferedImageReadImage, null, iioMetaDataImageToWrite);
writer.writeToSequence(iioImage, imageWriteParams);
}
} catch (Exception e) {
}
这是包含在读取的 .jpg 文件中的“javax_imageio_1.0”格式中包含的元数据:
<javax_imageio_1.0>
<Chroma>
<ColorSpaceType name="YCbCr"/>
<NumChannels value="3"/>
</Chroma>
<Compression>
<CompressionTypeName value="JPEG"/>
<Lossless value="FALSE"/>
<NumProgressiveScans value="1"/>
</Compression>
<Dimension>
<PixelAspectRatio value="1.0"/>
<ImageOrientation value="normal"/>
<HorizontalPixelSize value="0.16933332"/>
<VerticalPixelSize value="0.16933332"/>
</Dimension>
这是我的程序在 .tif 文件中写入的相同格式名称“javax_imageio_1.0”中包含的元数据
<javax_imageio_1.0>
<Chroma>
<ColorSpaceType name="RGB"/>
<NumChannels value="1"/>
<BlackIsZero/>
</Chroma>
<Compression>
<CompressionTypeName value="None"/>
</Compression>
<Data>
<PlanarConfiguration value="PixelInterleaved"/>
<SampleFormat value="UnsignedIntegral"/>
<BitsPerSample value="1"/>
<SampleMSB value="0"/>
</Data>
<Dimension>
<PixelAspectRatio value="1.0"/>
<HorizontalPixelSize value="5.905523436069756"/>
<VerticalPixelSize value="5.905523436069756"/>
</Dimension>
<Document>
<FormatVersion value="6.0"/>
</Document>
CompressionTypeName、HorizontalPixelSize 和 VerticalPixelSize 值丢失或更改。