【发布时间】:2026-02-21 22:55:01
【问题描述】:
使用找到的代码here,我编写了存储图像数据的代码,其中每个颜色分量都是一个浮点值。
// 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用这张图片成功写入数据?
【问题讨论】:
-
我认为您应该将图像转换为另一种类型。查看this question。
-
发布堆栈跟踪。始终提供准确的错误消息,而不是您认为它所说的内容的释义。
-
@user207421 没有堆栈跟踪,因为没有抛出异常。当
ImageIO.write函数失败时,writerFound为 false。 -
那么别猜了。 Javadoc 表示“使用支持给定格式的任意
ImageWriter将图像写入File。” '如果没有找到合适的作者,则返回 false。'没有任何关于“未能找到合适的作家为此自定义图像配置”。而且我看不出BufferedImage out = ...之后的三行有什么关系。他们来对地方了吗? -
@user207421 cmets 非常清楚:
data表示原始位图(浮点格式),然后将其批量复制到backingImageData,然后我尝试写入。我不是在“猜测”任何东西,“如果没有找到合适的作者,则返回错误”和“找不到合适的作者”是一回事,当我很清楚我的问题时,你为什么要纠缠我的问题的精确措辞?我在说什么?
标签: java image javax.imageio