Java 如何将基于浮点的图像写入文件?

Java 如何将基于浮点的图像写入文件?,java,image,javax.imageio,Java,Image,Javax.imageio,使用找到的代码,我编写了存储图像数据的代码,其中每个颜色分量都是浮点值 // Create a TYPE_FLOAT sample model (specifying how the pixels are stored) SampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_FLOAT, options.width, options.height, 4, options.width * 4, new i

使用找到的代码,我编写了存储图像数据的代码,其中每个颜色分量都是浮点值

// Create a TYPE_FLOAT sample model (specifying how the pixels are stored)
SampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_FLOAT, options.width, options.height, 4, options.width * 4, new int[]{0,1,2,3});
// ...and data buffer (where the pixels are stored)
DataBufferFloat buffer = new DataBufferFloat(options.width * options.height * 4);

// Wrap it in a writable raster
WritableRaster raster = Raster.createWritableRaster(sampleModel, buffer, null);

// Create a color model compatible with this sample model/raster (TYPE_FLOAT)
// Note that the number of bands must equal the number of color components in the 
// color space (3 for RGB) + 1 extra band if the color model contains alpha 
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel colorModel = new ComponentColorModel(colorSpace, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_FLOAT);

// And finally create an image with this raster
BufferedImage out = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
float[] backingImageData = buffer.getData();

FloatBuffer data = /*External Image Data Source...*/;
data.get(backingImageData); //Place data in image

boolean writerFound = ImageIO.write(out, "png", new File("C:\\out.png"));

但是,此代码失败,因为
ImageIO
无法为此自定义映像配置找到合适的写入程序(如调试时所示,
writerFound
false
)。如何获取
ImageIO
以成功使用此映像写入数据?

要将包含浮点值的映像写入文件,您需要:

a) 一种允许存储浮点值的格式。大多数图像格式,包括JPEG、PNG或GIF,当然不会。据我所知,最常用的文件格式是TIFF。不过,值得注意的是,浮点TIFF不是“基线”TIFF,因此并非所有TIFF软件都支持此类文件

b) 支持以浮点形式写入TIFF的ImageIO插件。我认为JAI可能会起作用,GeoTools肯定会起作用。我自己的插件目前支持读而不支持写浮点(但是如果你觉得有冒险精神的话,为它创建一个更改集应该是相当容易的)

排序后,以下内容应该可以工作(给定
out
与代码中的浮点图像相同):


我认为你应该把你的图像转换成另一种类型。签出。发布堆栈跟踪。始终提供准确的错误消息,而不是您对它所说内容的解释。@user207421没有堆栈跟踪,因为没有引发异常<当
ImageIO.write
函数失败时,code>writerFound为false。那么就不要猜测了。表示“使用支持给定格式的任意
ImageWriter
将图像写入
文件”
,“如果找不到合适的写入程序,则返回false”。与“无法为此自定义图像配置找到合适的写入程序”无关。我看不出
BufferedImage out=…
后面的三行与任何事情都有什么关系。它们在正确的位置吗?@user207421注释非常清楚:
data
表示原始位图(浮点格式),然后将其批量复制到
backingImageData
,然后我尝试编写该位图。我不是在“猜测”任何东西,“如果找不到合适的编写器,则返回false”和“找不到合适的编写器”是同一件事,当我说的很清楚时,你为什么要追问我问题的确切措辞?JPEG XR支持float16和float32。@cgohlke是正确的,但JPEG XR是完全不同的格式,而不是“正常”的JPEG。
ImageIO.write(out, "TIFF", new File("C:\\out.tif"));