我有一个 BufferedImage.TYPE_USHORT_GRAY
(DataBufferUShort
) 类型的 BufferedImage
,我想将其转换为 BufferedImage.TYPE_INT_RGB (DataBufferInt)
类型的 BufferedImage
。有什么建议我如何实现它吗?
最佳答案
如果您只想提取最小值/最大值/平均值,则无需转换 BufferedImage
:
// Assuming image is always TYPE_USHORT_GRAY
short[] values = ((DataBufferUShort) image.getRaster().getDataBuffer()).getData();
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
double mean;
for (int i = 0; i < values.length; i++) {
int unsignedValue = values[i] & 0xFFFF;
if (unsignedValue < min) {
min = unsignedValue;
}
if (unsignedValue > max) {
max = unsignedValue;
}
// ...etc. for mean and other values you like to extract. :-)
}
如果您想将图像转换为TYPE_INT_RGB
以用于显示目的(而不关心精度),您只需创建所需类型的新图像并在其上绘制原始图像即可:
BufferedImage display = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = display.createGraphics();
try {
g.drawImage(original, 0, 0, null);
}
finally {
g.dispose();
}
关于java - 想要将 DataBufferUShort 转换为 DataBufferInt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33164117/