【问题标题】:Convert byte array of data type TYPE_4BYTE_ABGR to BufferedImage将数据类型 TYPE_4BYTE_ABGR 的字节数组转换为 BufferedImage
【发布时间】:2015-05-19 02:20:47
【问题描述】:

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

【问题讨论】:

标签: java bufferedimage


【解决方案1】:

TYPE_4BYTE_ABGR 形式的字节数组创建BufferedImage 的最快方法是将数组包装在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);

【讨论】:

    【解决方案2】:

    可能效率不高,但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);
    

    【讨论】:

    • 感谢您的回复,但是我想要的是从 XXX 类型的字节数组创建一个 BufferedImage,而不是从现有图像,我更新了我的问题以使其清楚,您能帮忙吗?
    • @July 我也回答了,除非您知道宽度或高度,否则这是不可能的。为什么投反对票?编辑:我看到你改变了问题。
    • 我没有投反对票,也许是其他人投的?我编辑了问题以使其清楚,如果之前有误导,我深表歉意。
    • 再次感谢,稍后将尝试您编辑的答案并返回。
    猜你喜欢
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    相关资源
    最近更新 更多