且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Java中将16位灰度图像转换为RGB图像?

更新时间:2023-01-23 08:04:59

Incompl描述的getRGB/setRGB方法应该起作用.但是,如果我没记错的话,它的性能相当差(这些方法做了很多工作,在这种情况下是不必要的).我认为,使用新图像并让Java优化从BufferedImage.TYPE_USHORT_GRAYBufferedImage.TYPE_INT_RGB的转换会更快得多:

The getRGB/setRGB method described by Incompl should work. But, its performance is rather poor, if I remember correctly (these methods do a lot of work that is unnecessary in this case). I think it would be much faster to draw on the new image and let Java to optimize the conversion from BufferedImage.TYPE_USHORT_GRAY to BufferedImage.TYPE_INT_RGB:

int width = grayImage.getWidth();
int height = grayImage.getHeight()

BufferedImage newImage = new BufferedImage(
    width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = newImage.getGraphics();  
g.drawImage(grayImage, 0, 0, null);  
g.dispose();