簡體   English   中英

將數據類型TYPE_4BYTE_ABGR的字節數組轉換為BufferedImage

[英]Convert byte array of data type TYPE_4BYTE_ABGR to BufferedImage

我有一個類型為TYPE_4BYTE_ABGR的字節數組,我知道它的寬度和高度,我想將其更改為BufferedImage,有什么想法嗎?

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);

可能效率不是很高,但是可以通過以下方式將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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM