将数据类型TYPE_4BYTE_ABGR的字节数组转换为BufferedImage

时间:2015-05-14 12:56:50

标签: java bufferedimage

我有一个TYPE_4BYTE_ABGR类型的字节数组,我知道它的宽度和高度,我想把它改成BufferedImage,有什么想法吗?

2 个答案:

答案 0 :(得分:3)

BufferedImage形式从字节数组创建TYPE_4BYTE_ABGR的最快方法是将数组包装在DataBufferByte中并从中创建交错WritableRaster。这将确保没有额外的字节数组分配。然后从栅格中创建BufferedImage,并创建匹配的颜色模型:

public static void main(String[] args) {
    int width = 300;
    int height = 200;

    int samplesPerPixel = 4; // This is the *4BYTE* in TYPE_4BYTE_ABGR
    int[] bandOffsets = {3, 2, 1, 0}; // This is the order (ABGR) part in TYPE_4BYTE_ABGR

    byte[] abgrPixelData = new byte[width * height * samplesPerPixel];

    DataBuffer buffer = new DataBufferByte(abgrPixelData, abgrPixelData.length);
    WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height, samplesPerPixel * width, samplesPerPixel, bandOffsets, null);

    ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);

    BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
    System.out.println("image: " + image); // Should print: image: BufferedImage@<hash>: type = 6 ...
}

但是请注意,此图像将是“非托管”(某些硬件加速将被禁用),因为您可以直接访问像素阵列。

要避免这种情况,请创建不带像素的WritableRaster,然后将像素复制到其中。这将使用两倍的内存,但将保持图像“托管”,从而可能更好的显示性能:

// Skip creating the data buffer
WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, samplesPerPixel * width, samplesPerPixel, bandOffsets, null);
raster.setDataElements(0, 0, width, height, abgrPixelData);
// ...rest of code as above.

你甚至可以这样做(可能更熟悉):

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
WritableRaster raster = image.getRaster();
raster.setDataElements(0, 0, width, height, abgrPixelData);

答案 1 :(得分:1)

可能效率不高,但BufferedImage可以通过这种方式转换为其他类型:

public static BufferedImage convertToType(BufferedImage image, int type) {
    BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), type);
    Graphics2D graphics = newImage.createGraphics();
    graphics.drawImage(image, 0, 0, null);
    graphics.dispose();
    return newImage;
}

关于您要实施的方法,您必须知道图像的宽度或高度才能将byte[]转换为BufferedImage

编辑:
一种方法是将byte[]转换为int[](数据类型TYPE_INT_ARGB)并使用setRGB

int[] dst = new int[width * height];
for (int i = 0, j = 0; i < dst.length; i++) {
    int a = src[j++] & 0xff;
    int b = src[j++] & 0xff;
    int g = src[j++] & 0xff;
    int r = src[j++] & 0xff;
    dst[i] = (a << 24) | (r << 16) | (g << 8) | b;
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image.setRGB(0, 0, width, height, dst, 0, width);