这是我用来将原始像素数据转换为BufferedImage
。我的像素是 16 位签名的:
public static BufferedImage short2Buffered(short[] pixels, int width, int height) throws IllegalArgumentException {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY);
short[] imgData = ((DataBufferShort)image.getRaster().getDataBuffer()).getData();
System.arraycopy(pixels, 0, imgData, 0, pixels.length);
return image;
}
然后我使用 JAI 对生成的图像进行编码。告诉我您是否也需要代码。
编辑:感谢@Brent Nash,我极大地提高了速度answer https://stackoverflow.com/a/12062505/1098603关于类似的问题。
编辑:为了完整起见,这里是无符号 8 位的代码:
public static BufferedImage byte2Buffered(byte[] pixels, int width, int height) throws IllegalArgumentException {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
byte[] imgData = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
System.arraycopy(pixels, 0, imgData, 0, pixels.length);
return image;
}