将 8 位灰度图像字节数组转换为缓冲图像



我有一个字节数组,其中包含原始灰度8位图像的数据,我需要将其转换为BufferedImage。我试过做:

BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));

但是,生成的image对象为 null,这意味着我在这里做错了什么。

进行这种转换的正确方法是什么?

有两种

很好的方法可以做到这一点,具体取决于您的用例。

创建一个新的灰色图像,然后将数据复制到其中。这将使图像保持"托管"状态,这可能会带来更好的渲染性能(即在屏幕上)。但它需要两倍的内存,并将输入中的数据复制到图像中。

另一种方法是直接"围绕"现有像素数据创建灰色图像。这将更快,并且几乎不使用额外的堆,因为它避免了复制像素数据。但不会管理映像(因为后备阵列是公开的且可变的)。

下面演示了这两个选项:

int w = 640;
int h = 480;
byte[] imageBytes = new byte[w * h];
// 1 Keeps the image "managed" at the expense of twice the memory + a large array copy
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
image.getRaster().setDataElements(0, 0, w, h, imageBytes);
System.out.println("image: " + image);
// 2 Faster, and uses less memory, but will make the image "unmanaged"
ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
WritableRaster raster = Raster.createInterleavedRaster(new DataBufferByte(imageBytes, imageBytes.length), w, h, w, 1, new int[]{0}, null);
BufferedImage image2 = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
System.out.println("image2: " + image2);

如果图像数据不是线性灰色空间,则可以使用IndexColorModel将输入映射到所需的任何范围:

// Alternate, using IndexColorModel, if your input isn't in linear gray color space
int[] cmap = new int[256]; // TODO: Add ARGB packed colors here...
IndexColorModel icm = new IndexColorModel(8, 256, cmap, 0, false, -1, DataBuffer.TYPE_BYTE);
// As 1
BufferedImage image3 = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, icm);
image3.getRaster().setDataElements(0, 0, w, h, imageBytes);
System.out.println("image3: " + image3);
// As 2
BufferedImage image4 = new BufferedImage(icm, raster, cm.isAlphaPremultiplied(), null);
System.out.println("image4: " + image4);

Java

 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
 image.getRaster().setDataElements(0, 0, width, height, array));

科特林

val image = BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY)
image.raster.setDataElements(0, 0, width, height, byteArray )

我设法通过以下方式进行了640x480分辨率的转换:

BufferedImage image = new BufferedImage(640,480,BufferedImage.TYPE_BYTE_INDEXED);
int i = 0;
for(int y = 0; y < 480; y++)
{
    for(int x = 0; x < 640; x++)
    {
        int g = imageBytes[i++] & 0xFF;
        image.setRGB(x,y,new Color(g,g,g).getRGB());
    }
}

编辑:删除了无用的代码(感谢Marco13)

最新更新