gpt4 book ai didi

java - 想要将 DataBufferUShort 转换为 DataBufferInt

转载 作者:太空宇宙 更新时间:2023-11-04 13:19:59 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com